2014-09-24 89 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