[英]Entity Framework Many to Many Code FIrst
我有以下(簡化)的類:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFTest
{
public class TextContext : DbContext
{
public DbSet<People> People { get; set; }
public DbSet<File> Files { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
}
public class People
{
public People()
{
Files = new List<File>();
}
public int PeopleId { get; set; }
public string Nombre { get; set; }
public List<File> Files { get; set; }
}
public class File
{
public File()
{
Friends = new List<People>();
Foes = new List<People>();
}
public Int16 FileId { get; set; }
public List<People> Friends { get; set; }
public List<People> Foes { get; set; }
}
}
創建數據庫后,我希望它具有用於人員和文件的聯合表。 但是沒有創建任何東西,只有表People和表Files。
以下是實體數據模型的圖像
我希望該模型顯示多對多關系,因為每個人可能有很多文件,每個文件可能有很多人
我對EF還是很陌生,我知道我需要使用Fluent Api來配置關系,但是到目前為止,我嘗試過的所有代碼都失敗了。
謝謝您的幫助
嘗試使用virtual
關鍵字設置相關實體,以便EF可以覆蓋和實現這些導航屬性本身。 另外,我傾向於在屬性類上使用接口代替屬性。 您的來電...
public class People
{
public People()
{
Files = new List<File>();
}
public int PeopleId { get; set; }
public string Nombre { get; set; }
public virtual ICollection<File> Files { get; set; }
}
public class File
{
public File()
{
Friends = new List<People>();
Foes = new List<People>();
}
public Int16 FileId { get; set; }
public virtual ICollection<People> Friends { get; set; }
public virtual ICollection<People> Foes { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.