我在如何將這兩個表映射到一起,表上的TitleID
列映射到Title
表上的TitleID
表。表主鍵是NameTableID
/TitleTableID
並且是唯一的,NameID
,TitleID
(在兩個表上)都不是唯一的,當前記錄在DateEnd
列中找到空值。實體框架 - 映射多對多關係
public class Name
{
public int NameTableID { get; set; }
public int NameID { get; set; }
public int TitleID { get; set; } // Maps to Title.TitleID
public virtual Title Title { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public DateTime DateStart { get; set; }
public DateTime? DateEnd { get; set; }
}
public class Title
{
public int TitleTableID { get; set; }
public int TitleID { get; set; } // Maps to Name.TitleID
public string Description { get; set; }
public DateTime DateStart { get; set; }
public DateTime? DateEnd { get; set; }
}
我假設我需要一段代碼在我的DB Context類添加到OnModelCreating
,但我與映射/代碼掙扎,任何想法?
感謝, 馬丁
重複[EF 4.1 RC在EF CF中的多對多關係](http://stackoverflow.com/questions/5561652/ef-4-1-rc-many-to-many-relationship-in-ef- cf) – 2011-06-12 07:57:33
這已經被肯定地回答過 - 很多次。請搜索「EF多對多」 - 您將獲得很多有很好答案的點擊。或者看看[這篇博文在這裏](http://blogs.msdn.com/b/wriju/archive/2011/05/14/code-first-ef-4-1-building-many-to-many-relationship .aspx)的額外輸入 – 2011-06-12 07:58:04
是的,這已被回答了很多次,但僅限於主鍵。我已經繼承的數據庫不使用主關係。 – Martin 2011-06-12 10:34:57