Abh*_*hit 3 asp.net entity-framework-core .net-6.0
我的 DbContext 中有以下表:
public class Employee
{
[Key]
public int Id { get; set; }
public long EmployeeCode { get; set; }
public UserRole Role { get; set; }
public string? Salutation { get; set; }
public string? FirstName { get;set; }
public string? MiddleName { get;set; }
public string? LastName { get;set; }
public string? DisplayName { get; set; }
public virtual Address? Address { get; set; }
public byte[]? Image { get; set; } = null;
public DateTime Created { get; set; }
public UserStatus Status { get; set; }
public virtual Institution? Institution { get; set; }
public virtual List<Permission>? Permissions { get; set; }
public virtual List<EmploymentLog> EmploymentLogs { get; set; }
public bool HasAllPermission()
{
return HasPermission(Tasks.All);
}
public bool HasPermission(Tasks task)
{
//If no permissions were granted, return false
if (Permissions == null) return false;
//Check if user has all permission
if (Permissions.FirstOrDefault(p => p.Task == Tasks.All && p.Access) != null) return true;
//Check for the specific permisson
var permission = Permissions.FirstOrDefault(p => p.Task == task);
return permission != null && permission.Access;
}
public bool HasPermissionSuper(Tasks task)
{
//If no permissions were granted, return false
if (Permissions == null) return false;
//Check if user has all permission
if (Permissions.FirstOrDefault(p => p.Task == Tasks.Super && p.Access) != null) return true;
//Check for the specific permisson
var permission = Permissions.FirstOrDefault(p => p.Task == task);
return permission != null && permission.Access;
}
}
public class EmploymentLog
{
[Key]
public int Id { get; set; }
public virtual Employee Employee { get; set; }
public EmploymentEventType EventType { get; set; }
public virtual Employee InitiatedBy { get; set; }
public DateTime InitiatedOn { get; set; }
public EmploymentEventStatus Status { get; set; }
public virtual Employee ApprovedBy { get; set; }
public virtual DateTime ApprovedOn { get; set; }
}
//Model Builder:
modelBuilder.Entity<EmploymentLog>()
.HasOne(e => e.Employee)
.WithMany(e => e.EmploymentLogs)
.OnDelete(DeleteBehavior.NoAction);
Run Code Online (Sandbox Code Playgroud)
员工表保存了所有员工的记录,EmploymentLogs表保存了员工的雇佣记录的变化记录,例如员工第一次加入公司时,EmploymentLogs表中生成的记录,或者当员工被解雇时。员工晋升、降职、暂停或终止时,EmploymentLogs 表中会生成一条记录,在更新数据库时出现以下错误:
无法确定“Employee”类型的导航“EmploymentLog.ApprovedBy”表示的关系。手动配置关系,或者使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。
我以前使用过 EF6,这样的代码可以很好地运行,我做错了什么?
我以前使用过 EF6,这样的代码可以很好地运行,我做错了什么?
忘记 EF6。尽管存在一些相似之处,但 EF Core 是完全不同的系统,具有您需要遵循的自己的约定和规则。
该错误消息表明 EF Core 无法自动确定某些关系。当您对另一个实体具有多个导航属性时,通常会发生这种情况,并且在您的情况下,该实体具有 3 个到- 、和的EmploymentLog引用导航,而另一个实体具有到-的单个集合导航属性。EmployeeEmployeeInitiatedByApprovedByEmployeeeEmploymentLogEmploymentLogs
因此,您至少需要显式映射关联关系(导航属性对、外键、级联删除行为等)。您已经对其中一个进行了操作,现在对另外两个进行相同的操作,例如
// EmploymentLog relationships
// Employee (many-to-one)
modelBuilder.Entity<EmploymentLog>()
.HasOne(e => e.Employee) // reference
.WithMany(e => e.EmploymentLogs) // collection
.OnDelete(DeleteBehavior.NoAction);
// InitiatedBy (many-to-one)
modelBuilder.Entity<EmploymentLog>()
.HasOne(e => e.InitiatedBy) // reference
.WithMany() // no collection
.OnDelete(DeleteBehavior.NoAction);
// ApprovedBy (many-to-one)
modelBuilder.Entity<EmploymentLog>()
.HasOne(e => e.ApprovedBy) // reference
.WithMany() // no collection
.OnDelete(DeleteBehavior.NoAction);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4464 次 |
| 最近记录: |