[英]Entity-Framework 6, defining relationship by two IDs
Context: I have to change a part of a Model from a single value to a List of entries and need help defining the relationship between the two classes.上下文:我必须将模型的一部分从单个值更改为条目列表,并且需要帮助定义两个类之间的关系。
The Model (simplified) looks like this:模型(简化)如下所示:
public class Settings {
public Guid Id { get; set; }
public Guid NodeId { get; set; }
public Guid UserId { get; set; }
public Guid TemplateId { get; set; }
// plus the respective virtual properties and a few more irrelevant things.
}
Now the relationship has changed so that I have to handle multiple Templates, plus a few new flags, to something like this:现在关系发生了变化,所以我必须处理多个模板,加上一些新标志,如下所示:
public class Settings {
public Guid Id { get; set; }
public Guid NodeId { get; set; }
public Guid UserId { get; set; }
public ICollection<TemplateConfig> Configs { get; set; }
// ...
}
public class TemplateConfig {
public Guid NodeId { get; set; }
public Guid UserId { get; set; }
public Guid TemplateId { get; set; }
// and a few more flags
}
builder.Entity<TemplateConfig>().HasKey(t => new { t.NodeId, t.UserId, t.TemplateId });
Since many of my access will be directly on this list and based on either all entries for a Node, a User or a Template, I don't mind the redundancy in the IDs, Actually I prefer it.由于我的许多访问将直接在此列表中,并且基于节点、用户或模板的所有条目,因此我不介意 ID 中的冗余,实际上我更喜欢它。
I'd like to not have to add a SettingsId
just to be able to define this relationship, but rather do something like this:我想没有添加
SettingsId
只是为了能够定义这种关系,而是做这样的事情:
builder.Entity<Settings>()
.HasMany(s => s.Configs)
.HasForeignKey(s => new {s.NodeId, s.UserId});
So based on the shared ID-pair NodeId, UserId
.所以基于共享 ID 对
NodeId, UserId
。
But my EF knowledge is still very limited.但是我的EF知识还是很有限的。
I've tried it through the DbModelBuilder
and through ForeignKey
and Column
-attributes.我已经通过
DbModelBuilder
以及ForeignKey
和Column
DbModelBuilder
进行了尝试。 Different errors;不同的错误; all boiling down to a mismatch in the number of IDs between the Principal and the Dependant in the relationship.
所有这些都归结为关系中 Principal 和 Dependent 之间的 ID 数量不匹配。
Thank you for any help.感谢您的任何帮助。
You Settings
class has a single column primary key public Guid Id { get; set; }
您的
Settings
类有一个单列主键public Guid Id { get; set; }
public Guid Id { get; set; }
public Guid Id { get; set; }
. public Guid Id { get; set; }
. when you define relationship, EF core tries to bind 2 column foreign key .当您定义关系时,EF 核心会尝试绑定 2 列外键。
HasForeignKey(s => new {s.NodeId, s.UserId});
to 1 column primary key of 'Settings'
which is not possible.到
'Settings'
1 列主键,这是不可能的。 But you can tell EF to bind a foreign key to different columns which are called "Principal Key".但是您可以告诉 EF 将外键绑定到称为“主键”的不同列。 Try adding
.WithPrincipal()
in your code like this:尝试在您的代码中添加
.WithPrincipal()
,如下所示:
builder.Entity<Settings>()
.HasMany(s => s.Configs)
.WithOne()
.HasForeignKey(c => new {c.NodeId, c.UserId})
.HasPrincipalKey(s => new {s.NodeId, s.UserId});
Since EF 6 does not have .HasPrincipalKey()
, the principal columns should be specified with .Map()
:由于 EF 6 没有
.HasPrincipalKey()
,因此应使用.Map()
指定主体列:
builder.Entity<Settings>()
.HasMany(s => s.Configs)
.WithOne()
.Map(cs =>
{
cs.MapLeftKey("NodeId", "UserId");
cs.MapRightKey("NodeId", "UserId");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.