我正試圖實現與發生的事情非常相似的事情in this EF7 fluent API documentation,但事實並非如此。實體框架7 DbContext OnModelCreating爲ApplicationUser字段指定外鍵
我有一個模型,看起來像這樣:
public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string CreatedBy {get; set; }
public ApplicationUser CreatedByUser { get; set; }
}
我ApplicationUser類沒有在它有關的博文什麼。因此,連接並不需要雙向進行。
有人能告訴我如何我的情況我怎麼可以告訴大家,我想用包括基於在博文中CreatedBy場平了AspNetUsers表用戶名字段時填充CreatedByUser實體框架?
這是我希望能夠在我的倉庫做:
using (var blogContext = new BlogContext())
{
return blogContext .BlogPosts
.Include(bp => bp.CreatedByUser)
}
這是我最好的嘗試:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<BlogPost>()
.HasOne(fp => fp.CreatedByUser)
.WithMany()
.HasForeignKey(fp => fp.CreatedBy)
.IsRequired();
}
我覺得招這裏不加入一個參數.WithMany()因爲在我的模型中,我的ApplicationUser模型中沒有List屬性。
引起我的問題的主要原因是,默認情況下,EF正在嘗試使用Id字段作爲AspNetUsers表中的鍵。我想告訴它使用用戶名作爲關鍵,而不是guid。