Entity Framework 4.1中具有不同名称的唯一列和导航属性的流利映射/数据注释?

| 首先,是否有一种方法可以通过使用数据注释或流利的API来告诉EF 4.1列需要是唯一的? 其次,关于导航属性,我有2类Document和User。在文档类中,我具有OwnerId(UserId)的属性和Owner(User)的属性。如何告诉EF 4.1 OwnerId实际上是UserId,而Owner是回到User的导航属性? 文件类别:
public abstract class Document: BaseEntity
    {
        public bool IsActive { get; set; }
        public string Description { get; set; }
        //The UserId
        public Guid OwnerId { get; set; }
        //The User
        public User Owner { get; set; }

    }
    
已邀请:
实体框架完全不支持唯一键,因此第一个问题的答案为否。 除非您映射到外键命名不同的现有数据库,否则应将“ 1”自动识别为“ 2”的外键。在这种情况下,您可以使用例如:
public abstract class Document: BaseEntity
{
    public bool IsActive { get; set; }
    public string Description { get; set; }
    [Column(\"UserId\"), ForeignKey(\"Owner\")]
    public Guid OwnerId { get; set; }
    public User Owner { get; set; }
}
可能不需要外键数据批注,但是您可以使用它来将FK属性与Navigation属性显式配对。 在流利的映射中,您可以使用:
modelBuilder.Entity<Document>()
            .Property(d => d.OwnerId)
            .HasColumnName(\"UserId\");
modelBuilder.Entity<Document>()
            .HasRequired(d => d.Owner)
            .WithMany(...)
            .HasForeignKey(d => d.OwnerId);
    

要回复问题请先登录注册