我有以下类(class)
public class PestanasPorEntidad
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }
public int Orden { get; set; }
public string ClaseFontAwesome { get; set; }
public virtual Entidad Entidad { get; set; }
}
如您所见,该实体与 Entidad 相关。
我想要的是进行验证,以便 Order 列是唯一的,但在整个表中不是唯一的,只是针对同一实体。
这可以通过 DataAnnotations 实现吗?或者应该在 Controller 操作的服务器端完成?
最佳答案
如果您使用的是足够新版本的 Entity Framework ,则可以使用 Index
属性。您还需要向实体添加一个属性,该属性指定用于 Entitad
导航属性的列。
public class PestanasPorEntidad
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }
//Add this to specify a unique index for both Orden...
[Index("IX_OrdenAndEntitad", 1, IsUnique = true)]
public int Orden { get; set; }
public string ClaseFontAwesome { get; set; }
//...and also EntitadId
[Index("IX_OrdenAndEntitad", 2, IsUnique = true)]
public int EntitadId { get; set; }
public virtual Entidad Entidad { get; set; }
}
关于c# - Entity Framework 的独特验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566809/