簡體   English   中英

實體框架關聯結束的主體錯誤

[英]Entity Framework principal end of an association error

我有兩個模型

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類),我不確定它們是否應該與“模型”一起使用並制成表格還是只是常規類。 如果不應該將它們放入表中,那么它們應該放在項目中的什么位置?

再次感謝您的幫助! (請讓我知道是否需要澄清)

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

雇員不能存在雇佣關系。 根據外交關系法,如果沒有雇員,不得保存工作。 因此,在[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; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM