Entity Framework Core定义关系而不需要互相关联的属性

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

Entity Framework Core define relationship without reciprocal property

问题

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

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; }
}

需要注意的是,主键是 long 类型的 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);

还有一个需要注意的地方,必须在此处使用 Fluent 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-2.html
匿名

发表评论

匿名网友

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

确定