C# 实体框架主体结束关联错误

C# 实体框架主体结束关联错误,c#,entity-framework,scaffolding,C#,Entity Framework,Scaffolding,我有两种型号 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 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”之间关联的主体端。必须使用关系fluent API或数据注释显式配置此关联的主体端

谢谢你的帮助

编辑

假设
员工
模型具有以下内容:

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;}
}
LastName
类设为模型是否不正确?还是应该保持一种模式? 我如何使其成为一个资源类(即,不是要制作成表的模型) 此外,这种事情会打破员工/雇佣模式之间的关系吗

因为我仍然得到错误,不知道为什么;顺便说一下,我有很多这样的类,比如“
LastName
”示例,它们目前都在模型下,我不确定它们应该是模型还是资源类,如果是,我不知道它们应该放在哪里

这也是我的背景

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; }
}
公共类MyDbContext:DbContext
{
公共MyDbContext()
:base(“MyDbContext”)
{
}
公共数据库集雇员{get;set;}
公共数据库集就业{get;set;}
公共数据库集地址{get;set;}
公共数据库集部门{get;set;}
公共数据库集基本日期{get;set;}
公共数据库集BasicPhoneNumber{get;set;}
公共DbSet EmployeeIdentification{get;set;}
公共数据库集联邦标识{get;set;}
公共数据库集作业标题{get;set;}
公共数据库集LastName{get;set;}
公共数据库集状态{get;set;}
公共DbSet OfficeLocation{get;set;}
}
除了employee和employment之外的所有内容都是一个基本类(比如LastName类),我不确定它们是否应该使用“模型”并制作成表格,或者只是作为常规类。如果它们不应该被制成表格,那么在项目中它们应该去哪里


再次感谢您的帮助!(如果需要澄清,请告诉我)

我不认为
就业
字段需要
必需
属性,并且
外键
属性必须应用于
就业ID
字段。

就业不能与员工一起存在。根据《对外关系法》的规定,没有员工不得挽救雇佣关系。因此,在[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; }
    }
}
使用系统;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Linq;
命名空间测试控制台
{
公营雇员
{
公共int ID{get;set;}
[必需]
公共字符串名{get;set;}
公共字符串LastName{get;set;}
[必需]
公共虚拟就业就业详细信息{get;set;}
}
公营就业
{
[钥匙、外籍钥匙(“员工”)]
公共int ID{get;set;}
公共字符串部门{get;set;}
公用电话{get;set;}
公共虚拟员工{get;set;}
}
内部课程计划
{
公共TestConsoleDbContext MyDbContext{get;set;}
公共计划()
{
MyDbContext=newTestConsoledBContext();
}
私有静态void Main(字符串[]args)
{
var program=新程序();
var记录=来自program.MyDbContext.Employees中的p
选择新{p.EmploymentId,p.LastName,p.Employment.Department};
foreach(记录中的var r)
{
Console.WriteLine(“EmploymentID:{0}{1}部门:{2}”,r.EmploymentID,r.Department);
}
Console.ReadLine();
}
}
}
使用System.Data.Entity;
命名空间测试控制台
{
内部类TestDbContext:DbContext
{
公共IDbSet雇员{get;set;}
公共IDbSet就业{get;set;}
}
}

其他一切看起来都好吗?将虚拟字段放在每个模型的底部是对还是错?我不确定是否正确理解了您的问题。然而,无论您如何定义您的模型,我认为您不应该将LastName设置为ICollection。如果只是为了参数,我想是的,您可以定义任何类型的ICollection,如果您想存储到数据库中,那么必须构建模型。否则,您如何期望代码首先为您生成正确的数据库。
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; }
    }
}