我有两个型号
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'和'Bla.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类),我不确定他们是否应该使用“模型”并制作成表格或者只是常规课程。如果他们不应该被制作成表格,他们应该在哪里进入项目?
再次感谢您的帮助! (如果需要澄清,请告诉我)
答案 0 :(得分:1)
我认为Employment
字段不需要Required
属性,ForeignKey
字段必须应用于EmploymentID
字段。
答案 1 :(得分:1)
雇员不能存在就业。根据对外关系法,没有员工就不能保存就业。因此,在[ForeignKey]表示法的帮助下,您需要告知就业应保持员工关系。
就业取决于员工。因此,你必须告诉就业,你必须与校长联系。
因此,依赖类必须具有Employee 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; }
}
}