[英]EF Core multiple navigation properties to same table with fluent api
I searched a lot but no luck to find a solution to my entities.我进行了很多搜索,但没有找到解决我实体的方法。
I have 2 entities.我有 2 个实体。 Device & PersonEnterExit I have 2 Collection of PersonEnterExit in Device entity.设备和 PersonEnterExit 我在设备实体中有 2 个 PersonEnterExit 集合。 When I run database update it says me you have same entity with different name.当我运行数据库更新时,它说我有相同的实体但名称不同。 do manually name them.手动命名它们。 how do i do that?我怎么做? I want to do this with fluent api in onModelCreating.我想在 onModelCreating 中使用流利的 api 来做到这一点。
Devices.cs设备.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 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; }
}
}
Like you mentioned, you need to explicitly configure both relationships.就像您提到的那样,您需要明确配置这两种关系。 Otherwise, EF will get confused.否则,EF 会混淆。
build.Entity<PersonEnterExit>()
.HasOne(e => e.EnDevice)
.WithMany(d => d.PersonEnterExitEnDevice);
build.Entity<PersonEnterExit>()
.HasOne(e => e.ExDevice)
.WithMany(d => d.PersonEnterExitExDevice);
See if this helps.看看这是否有帮助。 You may need to explicitly configure foreign keys if this doesn't work.如果这不起作用,您可能需要显式配置外键。
You can use one of these您可以使用其中之一
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
to define the relationships.您可以使用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; }
}
}
Okey I combined two answers and its works now.好吧,我现在结合了两个答案及其作品。 I reply my question for maybe others use same approach.我回答我的问题,也许其他人使用相同的方法。 If i mistake please warn me.如果我错了,请警告我。 But it works now.但它现在有效。
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.