Entity Framework Core可以在没有互相关联属性的情况下定义关系。

huangapple go评论88阅读模式
英文:

Entity Framework Core define relationship without reciprocal property

问题

我有一个实体与另一个实体之间存在一对多的关系,使用主键进行连接。然而,我想知道是否可以在不显式声明“多”一方的情况下创建这种关系?

注意,主键是长整型的AltId属性,原因太复杂,无法在此解释。连接它们的外键是Guid类型的Id属性。

我没有问题配置Cars => Owners的关系:

  1. builder.Entity<CarModel>()
  2. .HasKey(x => x.AltId);
  3. builder.Entity<CarModel>()
  4. .HasOne(x => x.Owner)
  5. .WithMany(x => x.Cars)
  6. .HasPrincipalKey(x => x.Id)
  7. .HasForeignKey(x => x.Id);
  8. builder.Entity<OwnerModel>()
  9. .HasKey(x => x.AltId);

然而,我想避免在ColorModel中放置一个Cars属性。也许我想得太多了,但我不想从颜色导航到汽车,我只想知道特定汽车的颜色。(实际用例比汽车颜色稍微复杂一些,这只是为了简单起见)

这样做是行不通的,因为定义要求互惠性。除非我漏掉了什么,否则不能在没有在两端定义属性的情况下使用HasPrincipalKey

  1. builder.Entity<CarModel>()
  2. .HasOne(x => x.Color)
  3. .HasPrincipalKey(x => x.Id)
  4. .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?

  1. public class CarModel{
  2. public Guid Id { get; set; }
  3. public long AltId { get; set; }
  4. public OwnerModel Owner { get; set; }
  5. public ColorModel Color { get; set; }
  6. }
  7. public class ColorModel{
  8. public Guid Id { get; set; }
  9. public long AltId { get; set; }
  10. }
  11. public class OwnerModel{
  12. public Guid Id { get; set; }
  13. public long AltId { get; set; }
  14. public List&lt;CarModel&gt; Cars { get; set; }
  15. }

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:

  1. builder.Entity&lt;CarModel&gt;()
  2. .HasKey(x =&gt; x.AltId);
  3. builder.Entity&lt;CarModel&gt;()
  4. .HasOne(x =&gt; x.Owner)
  5. .WithMany(x =&gt; x.Cars)
  6. .HasPrincipalKey(x =&gt; x.Id)
  7. .HasForeignKey(x =&gt; x.Id);
  8. builder.Entity&lt;OwnerModel&gt;()
  9. .HasKey(x =&gt; 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?

  1. builder.Entity&lt;CarModel&gt;()
  2. .HasOne(x =&gt; x.Color)
  3. .HasPrincipalKey(x =&gt; x.Id)
  4. .HasForeignKey(x =&gt; 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文档的任何示例中都没有见过的。

  1. builder.Entity<CarModel>()
  2. .HasOne(x => x.Color)
  3. .WithMany()
  4. .HasPrincipalKey(x => x.Id)
  5. .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.

  1. builder.Entity&lt;CarModel&gt;()
  2. .HasOne(x =&gt; x.Color)
  3. .WithMany()
  4. .HasPrincipalKey(x =&gt; x.Id)
  5. .HasForeignKey(x =&gt; x.Id);

huangapple
  • 本文由 发表于 2023年8月9日 14:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定