❔ 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
Xeretis17mo ago
Take a look at this It has an IsUnique property Set that to true
Timo Martinson
Timo MartinsonOP17mo ago
does not work. 😦 "Index" is not an attribute class
Angius
Angius17mo 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
Pobiega17mo ago
assuming EF Core, please use IEntityConfiguraton classes instead of attributes for non-trivial configuration
Timo Martinson
Timo MartinsonOP17mo ago
I am using Entity Core with .Net Core !!!
Pobiega
Pobiega17mo ago
Good.
Accord
Accord17mo 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.

Did you find this page helpful?