英文:
Entity Framework Core define relationship without reciprocal property
问题
我有一个实体与另一个实体之间存在一对多的关系,使用主键进行连接。然而,我想知道是否可以在不显式声明“多”一方的情况下创建这种关系?
注意,主键是长整型的AltId
属性,原因太复杂,无法在此解释。连接它们的外键是Guid类型的Id
属性。
我没有问题配置Cars => Owners的关系:
builder.Entity<CarModel>()
.HasKey(x => x.AltId);
builder.Entity<CarModel>()
.HasOne(x => x.Owner)
.WithMany(x => x.Cars)
.HasPrincipalKey(x => x.Id)
.HasForeignKey(x => x.Id);
builder.Entity<OwnerModel>()
.HasKey(x => x.AltId);
然而,我想避免在ColorModel
中放置一个Cars
属性。也许我想得太多了,但我不想从颜色导航到汽车,我只想知道特定汽车的颜色。(实际用例比汽车颜色稍微复杂一些,这只是为了简单起见)
这样做是行不通的,因为定义要求互惠性。除非我漏掉了什么,否则不能在没有在两端定义属性的情况下使用HasPrincipalKey
。
builder.Entity<CarModel>()
.HasOne(x => x.Color)
.HasPrincipalKey(x => x.Id)
.HasForeignKey(x => x.Id);
另一个注意事项是,必须在这里使用流畅API进行操作,无法使用数据注释。实体模型在两个不同的上下文中使用,连接到两个不同的数据库,因此在模型中定义它们会引起冲突。
英文:
I have an entity that has a one-to-many relationship with another entity utilizing a Principle Key. However, I wonder if I can create this relationship without explicitly declaring the "many" side?
public class CarModel{
public Guid Id { get; set; }
public long AltId { get; set; }
public OwnerModel Owner { get; set; }
public ColorModel Color { get; set; }
}
public class ColorModel{
public Guid Id { get; set; }
public long AltId { get; set; }
}
public class OwnerModel{
public Guid Id { get; set; }
public long AltId { get; set; }
public List<CarModel> Cars { get; set; }
}
Caveat, also, the Primary Key is the long AltId
property, for reasons far too complex to explain here. The FK that connects these however is the Guid Id
property.
I had no issue configuring the Cars => Owners relationship:
builder.Entity<CarModel>()
.HasKey(x => x.AltId);
builder.Entity<CarModel>()
.HasOne(x => x.Owner)
.WithMany(x => x.Cars)
.HasPrincipalKey(x => x.Id)
.HasForeignKey(x => x.Id);
builder.Entity<OwnerModel>()
.HasKey(x => x.AltId);
However, I'd like to avoid putting a Cars
property in the ColorModel
. Maybe I'm overthinking it, but I don't want navigation from color -> car, I just want to know what color a particular car is. (Actual use case is slightly more complex than a car color, this is all just for simplicity)
This doesn't work, because the definitions require reciprocity. You can't do a HasPrincipalKey
without defining properties on both ends, unless I'm missing something?
builder.Entity<CarModel>()
.HasOne(x => x.Color)
.HasPrincipalKey(x => x.Id)
.HasForeignKey(x => x.Id);
Another Caveat, it has to be done here in the fluent API, I can't do it with data annotations. The entity models are used in two different contexts - connected to two different databases - so defining them in the model causes conflicts.
答案1
得分: 0
在写完整个问题之后,我想到可以尝试在WithMany
中不定义属性,这是我在EF Core文档的任何示例中都没有见过的。
builder.Entity<CarModel>()
.HasOne(x => x.Color)
.WithMany()
.HasPrincipalKey(x => x.Id)
.HasForeignKey(x => x.Id);
英文:
After writing out this entire question, it occurred to me to try the WithMany
without defining a property within it, something I don't recall seeing in any example from the EF Core documentation.
builder.Entity<CarModel>()
.HasOne(x => x.Color)
.WithMany()
.HasPrincipalKey(x => x.Id)
.HasForeignKey(x => x.Id);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论