[英]EF Core multiple navigation properties to same table with fluent api
我进行了很多搜索,但没有找到解决我实体的方法。
我有 2 个实体。 设备和 PersonEnterExit 我在设备实体中有 2 个 PersonEnterExit 集合。 当我运行数据库更新时,它说我有相同的实体但名称不同。 手动命名它们。 我怎么做? 我想在 onModelCreating 中使用流利的 api 来做到这一点。
设备.cs
using System;
using System.Collections.Generic;
namespace DesTech.Core.Entities
{
public class Device : BaseEntity
{
public bool? IsConnected { get; set; }
public string Name { get; set; }
public string Ip { get; set; }
public int Port { get; set; }
public DateTime? LastConnectTime { get; set; }
public DateTime? ChangeClockBatteryWarningTime { get; set; }
public bool HasFp { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<DeviceOptLog> DeviceOptLog { get; set; }
public virtual ICollection<DevicePerson> DevicePerson { get; set; }
public virtual ICollection<DeviceTimeZone> DeviceTimeZone { get; set; }
public virtual ICollection<PersonEnterExit> PersonEnterExitEnDevice { get; set; }
public virtual ICollection<PersonEnterExit> PersonEnterExitExDevice { get; set; }
public virtual ICollection<PersonPassLog> PersonPassLog { get; set; }
}
}
PersonEnterExit.cs
using System;
namespace DesTech.Core.Entities
{
public class PersonEnterExit : BaseEntity
{
public int Type { get; set; }
public int? PersonId { get; set; }
public DateTime? NoEnterTime { get; set; }
public int? EnDeviceId { get; set; }
public DateTime? EnVerifyDate { get; set; }
public string EnPlate { get; set; }
public int? ExDeviceId { get; set; }
public DateTime? ExVerifyDate { get; set; }
public virtual Device EnDevice { get; set; }
public virtual Device ExDevice { get; set; }
public virtual Person Person { get; set; }
}
}
就像您提到的那样,您需要明确配置这两种关系。 否则,EF 会混淆。
build.Entity<PersonEnterExit>()
.HasOne(e => e.EnDevice)
.WithMany(d => d.PersonEnterExitEnDevice);
build.Entity<PersonEnterExit>()
.HasOne(e => e.ExDevice)
.WithMany(d => d.PersonEnterExitExDevice);
看看这是否有帮助。 如果这不起作用,您可能需要显式配置外键。
您可以使用其中之一
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Device>()
.HasMany(a => a.PersonEnterExitEnDevice)
.WithOne(a => a.EnDevice)
.HasForeignKey(a => a.EnDeviceId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Device>()
.HasMany(a => a.PersonEnterExitExDevice)
.WithOne(a => a.ExDevice)
.HasForeignKey(a => a.ExDeviceId).OnDelete(DeleteBehavior.Restrict);
}
InverseProperty
来定义关系。namespace DesTech.Core.Entities
{
public class PersonEnterExit : BaseEntity
{
public int Type { get; set; }
public int? PersonId { get; set; }
public DateTime? NoEnterTime { get; set; }
public int? EnDeviceId { get; set; }
public DateTime? EnVerifyDate { get; set; }
public string EnPlate { get; set; }
public int? ExDeviceId { get; set; }
public DateTime? ExVerifyDate { get; set; }
[InverseProperty("PersonEnterExitEnDevice")]
public virtual Device EnDevice { get; set; }
[InverseProperty("PersonEnterExitExDevice")]
public virtual Device ExDevice { get; set; }
public virtual Person Person { get; set; }
}
}
namespace DesTech.Core.Entities
{
public class Device : BaseEntity
{
public bool? IsConnected { get; set; }
public string Name { get; set; }
public string Ip { get; set; }
public int Port { get; set; }
public DateTime? LastConnectTime { get; set; }
public DateTime? ChangeClockBatteryWarningTime { get; set; }
public bool HasFp { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<DeviceOptLog> DeviceOptLog { get; set; }
public virtual ICollection<DevicePerson> DevicePerson { get; set; }
public virtual ICollection<DeviceTimeZone> DeviceTimeZone { get; set; }
[InverseProperty("EnDevice")]
public virtual ICollection<PersonEnterExit> PersonEnterExitEnDevice { get; set; }
[InverseProperty("ExDevice")]
public virtual ICollection<PersonEnterExit> PersonEnterExitExDevice { get; set; }
public virtual ICollection<PersonPassLog> PersonPassLog { get; set; }
}
}
好吧,我现在结合了两个答案及其作品。 我回答我的问题,也许其他人使用相同的方法。 如果我错了,请警告我。 但它现在有效。
builder.HasMany(a => a.PersonEnterExitEnDevice)
.WithOne(a => a.EnDevice)
.HasForeignKey(a => a.EnDeviceId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasMany(a => a.PersonEnterExitExDevice)
.WithOne(a => a.ExDevice)
.HasForeignKey(a => a.ExDeviceId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(d => d.EnDevice)
.WithMany(e => e.PersonEnterExitEnDevice)
.HasForeignKey(d => d.EnDeviceId)
.HasConstraintName("FK_PersonEnterExit_DeviceEn");
builder.HasOne(d => d.EnLocation)
.WithMany(e => e.PersonEnterExitEnLocation)
.HasForeignKey(d => d.EnLocationId)
.HasConstraintName("FK_PersonEnterExit_LocationEx");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.