实体框架5代码首先使用额外属性进行自引用

时间:2013-03-05 06:40:29

标签: entity-framework-5

我的问题是,当我运行Add-Migration时,我的数据库模型最终会像这样。 FK四次而不是两次。

CreateTable(
            "dbo.IntressentIntressent",
            c => new
                {
                    ParentIntressentId = c.Int(nullable: false),
                    ChildIntressentId = c.Int(nullable: false),
                    Aktieantal = c.Int(nullable: false),
                    Agare = c.Boolean(nullable: false),
                    Firmatecknare = c.Boolean(nullable: false),
                    Registreringsanmalan = c.Boolean(nullable: false),
                    FirmatecknareGeneralfullmakt = c.Boolean(nullable: false),
                    Revisor = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => new { t.ParentIntressentId, t.ChildIntressentId })
            .ForeignKey("dbo.Intressent", t => t.ParentIntressentId)
            .ForeignKey("dbo.Intressent", t => t.ChildIntressentId)
            .ForeignKey("dbo.Intressent", t => t.ParentIntressentId)
            .ForeignKey("dbo.Intressent", t => t.ChildIntressentId)
            .Index(t => t.ParentIntressentId)
            .Index(t => t.ChildIntressentId)
            .Index(t => t.ParentIntressentId)
            .Index(t => t.ChildIntressentId);

我的课程看起来像这样。如果我在onmodelCreating中删除fk语句EF将为我添加它。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {   
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Entity<Entity.IntressentIntressent>().HasKey(k => new
       {k.ParentIntressentId, k.ChildIntressentId });

       modelBuilder.Entity<Entity.Intressent>().HasMany(c => c.ParentIntressenter)
                   .WithRequired().HasForeignKey(cp => cp.ParentIntressentId);

       modelBuilder.Entity<Entity.Intressent>().HasMany(p => p.ChildIntressenter)
                   .WithRequired().HasForeignKey(cp => cp.ChildIntressentId);

   }

实体看起来像这样

public class Intressent
{

    private ObservableCollection<IntressentIntressent> _parentIntressenter;
    public virtual ObservableCollection<IntressentIntressent> ParentIntressenter
    {
         get { return _parentIntressenter ?? (_parentIntressenter = new  
            ObservableCollection<IntressentIntressent>()); }
        set { _parentIntressenter = value; }
    }
}

public class IntressentIntressent
{
    [DisplayName("ParentIntressentId")]
    [Description("ParentIntressentId")]
    [Key, ForeignKey("ParentIntressent")]
    public int ParentIntressentId { get; set; }
    [DisplayName("ChildIntressentId")]
    [Description("ChildIntressentId")]
    [Key, ForeignKey("ChildIntressent")]
    public int ChildIntressentId { get; set; }


    [ForeignKey("ParentIntressent")]
    public Intressent ParentIntressent { get; set; }
    [ForeignKey("ChildIntressent")]
    public Intressent ChildIntressent { get; set; }

    [Required]
    [DisplayName("Aktieantal")]
    [Description("Aktieantal")]
    public int Aktieantal { get; set; }
}

1 个答案:

答案 0 :(得分:1)

你重复了这种关系。在IntressentIntressent中,您使用了数据注释来定义导航属性及其相关的外键。然后,您已为Intressent类定义了流畅映射,但该映射未反映关系相对侧的那些数据注释。这导致两次定义关系。用这个替换流畅的映射:

   modelBuilder.Entity<Entity.Intressent>()
               .HasMany(c => c.ParentIntressenter)
               .WithRequired(i => i.ParentInterssenter)
               .HasForeignKey(cp => cp.ParnetIntressentId);

   modelBuilder.Entity<Entity.Intressent>()
               .HasMany(p => p.ChildIntressenter)
               .WithRequired(i => i.ChildIntressent)
               .HasForeignKey(cp => cp.ChildIntressentId);

通过在WithRequired中定义相反的方法,可以将这些导航属性与单个关系配对。