的实体框架主要结束我有两个型号关联错误
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