❔ Make Entity Framework Property Unique

namespace Textopia.Models;

public class Blog(string slug, string? name = null, string? description = null)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Slug { get; set; } = slug;
public string? Name { get; set; } = name;
public string? Description { get; set; } = description;

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public ICollection<User>? Users { get; set; }
public ICollection<Post>? Posts { get; set; }
}
namespace Textopia.Models;

public class Blog(string slug, string? name = null, string? description = null)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Slug { get; set; } = slug;
public string? Name { get; set; } = name;
public string? Description { get; set; } = description;

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public ICollection<User>? Users { get; set; }
public ICollection<Post>? Posts { get; set; }
}
How can I make slugs unique!?
8 Replies
Xeretis
Xeretis15mo ago
Take a look at this It has an IsUnique property Set that to true
Timo Martinson
Timo MartinsonOP15mo ago
does not work. 😦 "Index" is not an attribute class
Angius
Angius15mo ago
Just to make sure: are you using the old-ass non-core Entity Framework, as used with .NET Framework? Or we're talking Entity Framework Core here, with .NET Core or .NET?
Pobiega
Pobiega15mo ago
assuming EF Core, please use IEntityConfiguraton classes instead of attributes for non-trivial configuration
Timo Martinson
Timo MartinsonOP15mo ago
I am using Entity Core with .Net Core !!!
Pobiega
Pobiega15mo ago
Good.
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server