【发布时间】:2021-08-22 01:36:03
【问题描述】:
我正在尝试为位置对象建模。位置可以运送到许多其他位置并从许多位置接收。位置也可以有一个父位置。如何通过实体框架在我的数据库中表示它? 这是我到目前为止所拥有的: 位置类:
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
public string ShortName { get; set; }
public Location ParentLocation { get; set; }
public int? ParentLocationId { get; set; }
public ICollection<Location> ShipToLocations { get; set; }
public ICollection<Location> ReceiveFromLocations { get; set; }
}
我的 DbContext 类中的 OnModelCreating 函数:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Location>(entity =>
{
entity.HasKey(x => x.Id);
entity.HasOne(x => x.ParentLocation);
entity.HasMany(x => x.ShipToLocations);
entity.HasMany(x => x.ReceiveFromLocations);
});
}
我似乎正在通过数据库获取 ParentLocationId 字段,这对我来说很有意义。 但是,ef 也会为数据库生成 LocationId 和 LocationId1 字段,这是没有意义的。按照我的理解,Entity Framework 应该生成某种形式的连接表,因为每个位置都有许多运送到位置的位置和许多从位置接收的位置,所有这些位置都是其他位置。
【问题讨论】:
-
TLDR;
.HasOne(x => x.ParentLocation).WithMany()&.HasMany(x => x.ShipToLocations).WithMany(x => x.ReceiveFromLocations).docs.microsoft.com/en-us/ef/core/modeling/…
标签: c# sql .net entity-framework