2013-03-05 48 views
0

嗨,我的問題是,當我運行添加遷移我的數據庫模型最終這樣。 FK四次而不是兩次。實體框架5代碼第一次自引用與額外的屬性

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); 

我的課程看起來像這樣。如果我刪除onmodel中的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

您有重複的關係。在您的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限定了相對的在配對的那些導航屬性到單個關係。

相關問題