0

EntityFramework試圖獲取不存在的列和外鍵屬性沒有幫助。EntityFramework試圖獲取不存在的列

在我的情況,我有兩個表與單一到多元的關係:

T_Blog 
- Id 
- FeaturedPost 

    T_Post 
- Id 
- BlogId 

,我定義的實體數據模型

public class T_Blog 
{ 
    [Key] 
    public int Id {get;set;} 
    public int? FeaturedPostId {get;set;} 

    [ForeignKey("FeaturedPostId")] 
    public virtual T_Post FeaturedPost {get;set;} 
    public virtual ICollection<T_Post> Posts {get;set;} 
} 

public class T_Post 
{ 
    [Key] 
    public int Id {get;set;} 
    [Required] 
    public int BlogId {get;set;} 

    [ForeignKey("BlogId")] 
    public T_Blog Blog {get;set;} 
} 

而且具有這種元數據定義的EF試圖獲取T_Blog_Id列每次我試圖執行db.T_Post.Where(...)。ToList();

我知道,只要我的T_Blog有兩個引用T_Post,那麼EF就是試圖獲取這兩個ID。

ps:是的,我知道這種類型的數據模型並不是最優的,但在我的情況下(至少現在)需要這種類型的非規範化。

如何正確定義第二個關係,以便EF知道要獲取什麼?

回答

3

您應該使用FluentAPI而不是註解,以避免那種映射失敗定義。

這裏是你的模型

public class BlogContext : DbContext 
{ 
    public BlogContext() 
     : base("name=BlogContext") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

     var blog = modelBuilder.Entity<T_Blog>(); 

     blog.HasKey(e => e.Id); 
     blog.HasOptional(e => e.FeaturedPost) 
      .WithMany() 
      .HasForeignKey(e => e.FeaturedPostId) 
      .WillCascadeOnDelete(false); 

     var post = modelBuilder.Entity<T_Post>(); 

     post.HasKey(e => e.Id); 
     post.HasRequired(e => e.Blog) 
      .WithMany(e => e.Posts) 
      .HasForeignKey(e => e.BlogId) 
      .WillCascadeOnDelete(true); 
    } 

    public virtual DbSet<T_Blog> Blogs { get; set; } 
    public virtual DbSet<T_Post> Posts { get; set; } 
} 

public class T_Blog 
{ 
    public int Id { get; set; } 

    public virtual ICollection<T_Post> Posts { get; set; } 

    public int? FeaturedPostId { get; set; } 
    public virtual T_Post FeaturedPost { get; set; } 
} 

public class T_Post 
{ 
    public int Id { get; set; } 

    public int? BlogId { get; set; } 
    public virtual T_Blog Blog { get; set; } 
} 

樣品,並自動生成的遷移

public partial class InitialCreate : DbMigration 
{ 
    public override void Up() 
    { 
     CreateTable(
      "dbo.T_Blog", 
      c => new 
       { 
        Id = c.Int(nullable: false, identity: true), 
        FeaturedPostId = c.Int(), 
       }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.T_Post", t => t.FeaturedPostId) 
      .Index(t => t.FeaturedPostId); 

     CreateTable(
      "dbo.T_Post", 
      c => new 
       { 
        Id = c.Int(nullable: false, identity: true), 
        BlogId = c.Int(nullable: false), 
       }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.T_Blog", t => t.BlogId, cascadeDelete: true) 
      .Index(t => t.BlogId); 

    } 

    public override void Down() 
    { 
     DropForeignKey("dbo.T_Blog", "FeaturedPostId", "dbo.T_Post"); 
     DropForeignKey("dbo.T_Post", "BlogId", "dbo.T_Blog"); 
     DropIndex("dbo.T_Post", new[] { "BlogId" }); 
     DropIndex("dbo.T_Blog", new[] { "FeaturedPostId" }); 
     DropTable("dbo.T_Post"); 
     DropTable("dbo.T_Blog"); 
    } 
} 
+0

流利的API是偉大的,以避免這樣的困惑。我會給它一個鏡頭。到目前爲止,只要需要更少的代碼寫入,並且(更重要的是)在更改某些內容時重構的次數更少,屬性就會更方便。 –

1

在我的研究中失去了一個小時後,我找到了答案。如果EF是無法定義的映射,你應該不只是ForeignKey而且InverseProperty

public class T_Blog 
{ 
    [Key] 
    public int Id {get;set;} 
    public int? FeaturedPostId {get;set;} 

    [ForeignKey("FeaturedPostId")] 
    public virtual T_Post FeaturedPost {get;set;} 
    [InverseProperty("Blog")] 
    public virtual ICollection<T_Post> Posts {get;set;} 
} 

找到here

相關問題