【发布时间】:2021-09-18 04:49:12
【问题描述】:
我需要配置可选/可为空的外键。在我的场景中,ParkingAttendant 与 HandHeldDevice 的关系为 0:*。我已经放了配置类但不确定是否正确?
public class HandheldDevice
{
// other properties
public int ParkingAttendantId { get; set; }
public ParkingAttendant ParkingAttendants { get; set; }
}
public class ParkingAttendant
{
public ParkingAttendant()
{
this.HandheldDevices = new HashSet<HandheldDevice>();
}
// other properties
public ICollection<HandheldDevice> HandheldDevices { get; set; }
}
配置类
public class HandHeldDeviceConfiguration : IEntityTypeConfiguration<HandheldDevice>
{
public void Configure(EntityTypeBuilder<HandheldDevice> builder)
{
builder.ToTable("mytable", "dbo");
builder.HasKey(column => column.HandHeldId);
builder
.HasOne(handHeldDevice => handHeldDevice.ParkingAttendants)
.WithMany(pakingAttendant => pakingAttendant.HandheldDevices)
.HasForeignKey(handHeldDevice => handHeldDevice.ParkingAttendantId)
.IsRequired(false);
}
}
【问题讨论】:
-
这里看不出有什么问题,你这里有一对多(或零)的关系
标签: c# .net-core entity-framework-core