2014-09-24 90 views
1

的實體框架主要結束我有兩個型號關聯錯誤

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; } 
} 

一切都是基本類(如姓氏類),我不知道他們是否應該與「模型「並製作成桌子或者只是一邊正規的課程。如果他們不應該被製成表格,他們應該在項目中去哪裏?

再次感謝您的幫助! (請讓我知道如果有什麼需要澄清)

回答

1

就業不能與員工一起存在。根據對外關係法,如果沒有員工,就不能挽救就業。因此,在[ForeignKey]符號的幫助下,您需要告知就業,應保持員工關係。

就業依賴於員工。因此,你必須告訴就業,你必須與校長有聯繫。

因此,依賴類必須具有員工id參考。

using System; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 

namespace TestConsole 
{ 
    public class Employee 
{ 
    public int ID { get; set; } 

    [Required] 
    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    [Required] 
    public virtual Employment EmploymentDetails { get; set; } 
} 

public class Employment 
{ 
    [Key, ForeignKey("Employee")] 
    public int ID { get; set; } 

    public string Department { get; set; } 

    public int OfficePhone { get; set; } 

    public virtual Employee Employee { get; set; } 
    } 


    internal class Program 
    { 
     public TestConsoleDbContext MyDbContext { get; set; } 

     public Program() 
     { 
      MyDbContext = new TestConsoleDbContext(); 
     } 

     private static void Main(string[] args) 
     { 

      var program = new Program(); 

      var records = from p in program.MyDbContext.Employees 
          select new { p.EmploymentId, p.LastName, p.Employment.Department }; 

      foreach (var r in records) 
      { 
       Console.WriteLine("EmploymentID: {0} {1} Department: {2}", r.EmploymentId, r.Department); 
      } 

      Console.ReadLine(); 


     } 
    } 
} 

using System.Data.Entity; 

namespace TestConsole 
{ 
    internal class TestDbContext : DbContext 
    { 
     public IDbSet<Employee> Employees { get; set; } 
     public IDbSet<Employment> Employments { get; set; } 
    } 
} 
+0

我不確定我是否正確地得到了您的問題。不過,無論你如何定義你的模型,我都不認爲你應該讓LastName成爲ICollection。如果只是爲了參數,我認爲是的,你可以定義任何類型的ICollection,如果你想存儲到數據庫中,那麼模型必須被構建。否則,您如何期望代碼首先爲您生成正確的數據庫。 – codebased 2014-09-24 03:45:07

1

我不認爲Employment場需要Required屬性和ForeignKey屬性必須被應用到EmploymentID領域。

+0

其他一切看起來不錯嗎?虛擬領域在每個模型的底部是正確的還是錯誤的? – 2014-09-24 02:10:09