Skip to main content
deleted 71 characters in body
Source Link
ViRuSTriNiTy
  • 5.2k
  • 2
  • 35
  • 63

The OP is asking about whether it is possible to add an Attribute to an Entity class for a Unique Key. The short answer is that it IS possible, but not an out-of-the-box feature from the EF Core Team. If you'd like to use an Attribute to add Unique Keys to your Entity Framework Core entity classes, you can do what I've posted herehere

public class Company { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid CompanyId { get; set; } [Required] [UniqueKey(groupId: "1", order: 0)] [StringLength(100, MinimumLength = 1)] public string CompanyName { get; set; } [Required] [UniqueKey(groupId: "1", order: 1)] [StringLength(100, MinimumLength = 1)] public string CompanyLocation { get; set; } } 

The OP is asking about whether it is possible to add an Attribute to an Entity class for a Unique Key. The short answer is that it IS possible, but not an out-of-the-box feature from the EF Core Team. If you'd like to use an Attribute to add Unique Keys to your Entity Framework Core entity classes, you can do what I've posted here

public class Company { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid CompanyId { get; set; } [Required] [UniqueKey(groupId: "1", order: 0)] [StringLength(100, MinimumLength = 1)] public string CompanyName { get; set; } [Required] [UniqueKey(groupId: "1", order: 1)] [StringLength(100, MinimumLength = 1)] public string CompanyLocation { get; set; } } 

The OP is asking about whether it is possible to add an Attribute to an Entity class for a Unique Key. The short answer is that it IS possible, but not an out-of-the-box feature from the EF Core Team. If you'd like to use an Attribute to add Unique Keys to your Entity Framework Core entity classes, you can do what I've posted here

public class Company { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid CompanyId { get; set; } [Required] [UniqueKey(groupId: "1", order: 0)] [StringLength(100, MinimumLength = 1)] public string CompanyName { get; set; } [Required] [UniqueKey(groupId: "1", order: 1)] [StringLength(100, MinimumLength = 1)] public string CompanyLocation { get; set; } } 
Source Link

The OP is asking about whether it is possible to add an Attribute to an Entity class for a Unique Key. The short answer is that it IS possible, but not an out-of-the-box feature from the EF Core Team. If you'd like to use an Attribute to add Unique Keys to your Entity Framework Core entity classes, you can do what I've posted here

public class Company { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid CompanyId { get; set; } [Required] [UniqueKey(groupId: "1", order: 0)] [StringLength(100, MinimumLength = 1)] public string CompanyName { get; set; } [Required] [UniqueKey(groupId: "1", order: 1)] [StringLength(100, MinimumLength = 1)] public string CompanyLocation { get; set; } }