Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法确定主体端_C#_Entity Framework - Fatal编程技术网

C# 无法确定主体端

C# 无法确定主体端,c#,entity-framework,C#,Entity Framework,重要提示:我发现了很多关于“无法”的类似问题 确定关系的主要目的。多次添加 实体在SO中可能存在相同的主键异常。我读了它,明白了什么 它的意思是什么?为什么会发生。 但是,我找不到问题隐藏在哪里,特别是在我的模型中。如果你能指出我做错了什么,我将不胜感激。 此处的模型: public class Faculty : Entity { public string Name { get; set; } public string Acronym { get; set; } p

重要提示:我发现了很多关于“无法”的类似问题 确定关系的主要目的。多次添加 实体在SO中可能存在相同的主键异常。我读了它,明白了什么 它的意思是什么?为什么会发生。

但是,我找不到问题隐藏在哪里,特别是在我的模型中。如果你能指出我做错了什么,我将不胜感激。 此处的模型:

public class Faculty : Entity
{
    public string Name { get; set; }
    public string Acronym { get; set; }
    public string Description { get; set; }

    public ICollection<Department> Departments { get; set; }
    public ICollection<Specialty> Specialties { get; set; }
    public ICollection<Student> Students { get; set; }
    public ICollection<Employee> Employees { get; set; }
}

public class Department : Entity
{
    public string Name { get; set; }
    public string Acronym { get; set; }
    public string Description { get; set; }
    public Guid ? FacultyId { get; set; }
    public Faculty Faculty { get; set; }

    public ICollection<Subject> Subjects { get; set; }
    public ICollection<Employee> Employees { get; set; }
    public ICollection<Specialty> Specialties { get; set; }
}

public class Specialty : Entity
{
    public string Name { get; set; }
    public string Acronym { get; set; }
    public string Description { get; set; }
    public ICollection<Student> Students { get; set; }
    public Guid FacultyId { get; set; }
    public Faculty Faculty { get; set; }
    public Guid DepartmentId { get; set; }
    public Department Department { get; set; } 
}
公共课堂教员:实体
{
公共字符串名称{get;set;}
公共字符串首字母缩略词{get;set;}
公共字符串说明{get;set;}
公共ICollection部门{get;set;}
公共ICollection属性{get;set;}
公共ICollection学生{get;set;}
公共ICollection雇员{get;set;}
}
公共类部门:实体
{
公共字符串名称{get;set;}
公共字符串首字母缩略词{get;set;}
公共字符串说明{get;set;}
公共Guid?FacultyId{get;set;}
公共学院{get;set;}
公共ICollection主题{get;set;}
公共ICollection雇员{get;set;}
公共ICollection属性{get;set;}
}
公共类专业:实体
{
公共字符串名称{get;set;}
公共字符串首字母缩略词{get;set;}
公共字符串说明{get;set;}
公共ICollection学生{get;set;}
公共Guid FacultyId{get;set;}
公共学院{get;set;}
公共Guid部门ID{get;set;}
公共部门部门{get;set;}
}
配置:这里的映射

public class DepartmentConfiguration : EntityConfiguration<Department>
{
    public DepartmentConfiguration()
    {
        Property(p => p.Name).IsRequired().HasMaxLength(200);
        Property(p => p.Acronym).IsRequired().HasMaxLength(5);
        Property(p => p.Description).HasMaxLength(1000);

        HasMany(p => p.Subjects).WithRequired(p => p.Department);
        HasMany(p => p.Employees).WithOptional(p => p.Department);
        HasMany(p => p.Specialties).WithRequired(p => p.Department);
    }
}

public class SpecialtyConfiguration : EntityConfiguration<Specialty>
{
    public SpecialtyConfiguration()
    {
        Property(p => p.Name).IsRequired().HasMaxLength(200);
        Property(p => p.Acronym).IsRequired().HasMaxLength(5);
        Property(p => p.Description).HasMaxLength(1000);
        HasMany(p => p.Students).WithRequired(p => p.Specialty);
    }
}

public class FacultyConfiguration : EntityConfiguration<Faculty>
{
    public FacultyConfiguration() : base()
    {
        Property(p => p.Name).IsRequired().HasMaxLength(200);
        Property(p => p.Acronym).IsRequired().HasMaxLength(5);
        Property(p => p.Description).HasMaxLength(1000);

        HasMany(p => p.Departments).WithOptional(p => p.Faculty);
        HasMany(p => p.Specialties).WithRequired(p => p.Faculty);
        HasMany(p => p.Employees).WithOptional(p => p.Faculty);
        HasMany(p => p.Students).WithRequired(p => p.Faculty);
    }
}
公共类部门配置:EntityConfiguration
{
公共部门配置()
{
属性(p=>p.Name).IsRequired().HasMaxLength(200);
属性(p=>p.Acronym).IsRequired().HasMaxLength(5);
属性(p=>p.Description).HasMaxLength(1000);
有许多(p=>p.Subjects)。需要(p=>p.Department);
有很多员工(p=>p.Employees),可选(p=>p.Department);
有许多专业(p=>p.Specials)。需要(p=>p.Department);
}
}
公共类特殊配置:EntityConfiguration
{
公共专业配置()
{
属性(p=>p.Name).IsRequired().HasMaxLength(200);
属性(p=>p.Acronym).IsRequired().HasMaxLength(5);
属性(p=>p.Description).HasMaxLength(1000);
有很多(p=>p.Students)。需要(p=>p.Speciality);
}
}
公共类功能配置:实体配置
{
public FacultyConfiguration():base()
{
属性(p=>p.Name).IsRequired().HasMaxLength(200);
属性(p=>p.Acronym).IsRequired().HasMaxLength(5);
属性(p=>p.Description).HasMaxLength(1000);
有许多(p=>p.Departments)。有可选的(p=>p.Faculty);
有许多专业(p=>p.Specials)。需要(p=>p.Faculty);
拥有许多(p=>p.Employees)。可选(p=>p.Faculty);
有很多(p=>p.Students)。需要(p=>p.Faculty);
}
}
例外是关于部门和专业之间的关系

无法确定服务器的主端 “EMIS.DAL.Context.Department\u Specialties”关系。多次添加 实体可以具有相同的主键


解决了这个问题。映射中没有错误。我忘了,使用
DatabaseGeneratedOption.Identity
PK只在将数据保存到数据库时添加。然而,我试图向ID等于null的部门添加专业,直到它在DB中不存在

有时这是必要的。您可以在一个事务中包装多个相关的数据库操作。在事务内部,您调用SaveChanges(),db将表现为已更新(您可以访问生成的ID等),但除非调用Commit(),否则当事务对象超出范围时,所有更改都将回滚

比如说,

using (var transaction = myDbContext.BeginTransaction())
{
  InsertFirstThing();
  myDbContext.SaveChanges();

  InsertSecondThing();
  myDbContext.SaveChanges();

  if (EverythingWasSuccessful())
  {
    transaction.Commit();
  }
}