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

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

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&lt;CarModel&gt; 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&lt;CarModel&gt;()
    .HasKey(x =&gt; x.AltId);
builder.Entity&lt;CarModel&gt;()
    .HasOne(x =&gt; x.Owner)
    .WithMany(x =&gt; x.Cars)
    .HasPrincipalKey(x =&gt; x.Id)
    .HasForeignKey(x =&gt; x.Id);


builder.Entity&lt;OwnerModel&gt;()
    .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?

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

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&lt;CarModel&gt;()
    .HasOne(x =&gt; x.Color)
    .WithMany()
    .HasPrincipalKey(x =&gt; x.Id)
    .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:

确定