的實體框架主要結束我有兩個型號關聯錯誤
class Employee {
[Key]
public int ID {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public int EmploymentID {get;set;}
[Required, ForeignKey("Employment")]
public virtual Employment Employment {get;set;}
}
class Employment {
[Key, ForeignKey("Employee")]
public int ID {get;set;}
public string Department {get;set;}
public int OfficePhone {get;set;}
public int EmployeeID {get;set;}
public virtual Employee Employee {get;set;}
}
基本上每個員工都有在就業類就業信息。我不確定是否需要[Required]
註釋,我也不知道是否將[ForeignKey]
註釋放在正確的位置。
的問題是,當我嘗試創建一個新的架式項目,它給了我這個錯誤:
無法確定類型「Bla.Models.Employee」和「布拉之間的關聯的主要終點.Models.Employment」。該關聯的主要目的必須使用關係流暢API或數據註釋來顯式配置。
感謝您的幫助
編輯
假設Employee
模型,而不是執行以下操作:
class Employee {
[Key]
public int ID {get;set;}
public string FirstName {get;set;}
//note the return value is an ICollection object
public ICollection<LastName> LastName {get;set;}
public int EmploymentID {get;set}
public virtual Employment Employment {get;set;}
}
和Employment
模式保持不變, 和LastName
字段有以下類別
class LastName {
public string EmployeeLastName {get;set;}
//assuming last name can change, and employee had a different last name at some point
public int year{get;set;}
}
將LastName
類作爲模型是不正確的嗎?還是應該保持模式? 我怎樣才能使它只是一個資源類別(即不是一個表格模型) 進一步,這種事情會打破僱員/就業模式之間的關係?
因爲我仍然得到錯誤,不知道爲什麼;順便說一下,我有很多這樣的類,比如「LastName
」示例,它們都在模型中,我不確定它們是模型還是某個資源類,如果是,我不知道在那裏,他們都應該去
也繼承人我的DbContext比員工和就業等
public class MyDbContext : DbContext
{
public MyDbContext()
: base("MyDbContext")
{
}
public DbSet<Employee> Employees { get; set; }
public DbSet<Employment> Employments { get; set; }
public DbSet<BasicAddress> Adresses { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<BasicDate> BasicDate { get; set; }
public DbSet<BasicPhoneNumber> BasicPhoneNumber { get; set; }
public DbSet<EmployeeIdentification> EmployeeIdentification { get; set; }
public DbSet<FederalIdentification> FederalIdentification { get; set; }
public DbSet<JobTitle> JobTitle { get; set; }
public DbSet<LastName> LastName { get; set; }
public DbSet<MaritalStatus> MaritalStatus { get; set; }
public DbSet<OfficeLocation> OfficeLocation { get; set; }
}
一切都是基本類(如姓氏類),我不知道他們是否應該與「模型「並製作成桌子或者只是一邊正規的課程。如果他們不應該被製成表格,他們應該在項目中去哪裏?
再次感謝您的幫助! (請讓我知道如果有什麼需要澄清)
我不確定我是否正確地得到了您的問題。不過,無論你如何定義你的模型,我都不認爲你應該讓LastName成爲ICollection。如果只是爲了參數,我認爲是的,你可以定義任何類型的ICollection,如果你想存儲到數據庫中,那麼模型必須被構建。否則,您如何期望代碼首先爲您生成正確的數據庫。 – codebased 2014-09-24 03:45:07