2010-12-22 66 views
0

我有以下代碼:的EntityFramework FluentAPI馬平問題

namespace DynamicAssembly { 
    using System; 
    using System.Collections.Generic; 
    using System.Collections; 
    using System.Data.Objects; 
    using System.Data.EntityClient; 
    using System.Data.Entity; 
    using System.ComponentModel.DataAnnotations; 
    using System.Data.Entity.ModelConfiguration; 
    using System.Data.Entity.Infrastructure; 


    [Table("eElementDef1")] 
    public class ElementDef1 { 

      public int pID { get; set; } 
       public virtual ElementDef2 Pointer_EntityDef2 { get; set; } 
       public virtual ElementDef1 Poniter_EntityDef1 { get; set; } 

     public ElementDef1() { 
     } 
    } 

    [Table("eElementDef2")] 
    public class ElementDef2 { 


      public int pID { get; set; } 
      public String Name { get; set; } 

     public ElementDef2() { 
     } 
    } 

    public class Context : System.Data.Entity.DbContext { 

      public DbSet<ElementDef1> ElementDef1 
      { 
       get;set; 
      } 
      public DbSet<ElementDef2> ElementDef2 
      { 
       get;set; 
      } 

     protected override void OnModelCreating(ModelBuilder modelBuilder) 
     { 
        modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID); 

modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2) 
                     .WithMany() 
                     .IsIndependent() 
                     .Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2")); 


modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1) 
                     .WithMany() 
                     .IsIndependent() 
                     .Map(m => m.MapKey(p => p.pID, "Poniter_EntityDef1")); 

      modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID); 



     } 

     public Context() : 
       base("DynamicDefinitionConnection") { 
     } 
    } 

} 

問題是與該映射 modelBuilder.Entity()HasRequired(p值=> p.Pointer_EntityDef2) .WithMany() .IsIndependent。 () .Map(m => m.MapKey(p => p.pID,「Pointer_EntityDef2」));

的分貝不尊重德慣例,FK不是Pointer_EntityDef2pId 是否有可能在我的類只有1個屬性(類型EntityDef2的Pointer_EntityDef2),而不是1個屬性id和一個用於對象(此一個工程確定),以及該財產具有FK名稱?而映射應該如何呢?

回答

0

這一口流利的API給你所需要的模式(不需要調用IsIndependent後地圖()()):

[Table("eElementDef1")] 
public class ElementDef1 
{ 
    public int pID { get; set; } 
    public virtual ElementDef2 Pointer_EntityDef2 { get; set; } 
    public virtual ElementDef1 Poniter_EntityDef1 { get; set; } 
} 

[Table("eElementDef2")] 
public class ElementDef2 
{ 
    public int pID { get; set; } 
    public String Name { get; set; } 
} 

public class StackoverflowContext : DbContext 
{ 
    public DbSet<ElementDef1> ElementDef1s { get; set; } 
    public DbSet<ElementDef2> ElementDef2s { get; set; } 

    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID); 
     modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID); 

     modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2) 
             .WithMany() 
             .IsIndependent(); 

     modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1) 
             .WithMany() 
             .IsIndependent(); 
    } 
}