重要:我发现了很多关于无法确定关系的主要目的。已添加多个实体在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; }
}
[配置:这里的映射
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);
}
}
并且关于部门与专业之间的关系有例外规定。
无法确定“ EMIS.DAL.Context.Department_Specialties”关系。已添加多个实体可能具有相同的主键。
解决了这个问题。映射中没有错误。我忘记了,仅在将数据保存到数据库时才使用DatabaseGeneratedOption.Identity
PK。但是,我试图将ID等于null的Specialty添加到Department中,直到它在数据库中不存在为止。
有时这是必要的。您可以在一个事务中包装多个相关的数据库操作。在事务内部,您调用SaveChanges(),并且数据库将像更新数据库一样工作(您可以访问生成的ID等),但是除非您调用Commit(),否则所有更改将在事务对象退出后回滚。范围。
例如,
using (var transaction = myDbContext.BeginTransaction())
{
InsertFirstThing();
myDbContext.SaveChanges();
InsertSecondThing();
myDbContext.SaveChanges();
if (EverythingWasSuccessful())
{
transaction.Commit();
}
}