【发布时间】:2017-07-07 15:27:19
【问题描述】:
我有以下类(我无法更新、添加属性或添加注释):
public class ApprovalRuleset
{
public Guid Id { get; set; }
public List<ApprovalRule> ApprovalRules { get; protected internal set; }
}
public class ApprovalRule
{
public Guid Id { get; set; }
public string Value { get; protected internal set; }
}
我正在尝试使用 Entity Framework 6 编写一些 Fluent API 代码以将它们映射到两个表。
这是 ApprovalRule 配置:
public class ApprovalRuleEntityConfiguration : EntityTypeConfiguration<ApprovalRule>
{
public ApprovalRuleEntityConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.Value).IsRequired().HasMaxLength(450);
}
}
到目前为止,我得到了:
public class ApprovalRulesetEntityConfiguration : EntityTypeConfiguration<ApprovalRuleset>
{
public ApprovalRulesetEntityConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.ApprovalRules);
}
}
表“ApprovalRules”上的外键与列 无法创建“ApprovalRuleset_Id”,因为主键 无法确定列。使用 AddForeignKey fluent API 完全指定外键。
public class ApprovalRulesetEntityConfiguration : EntityTypeConfiguration<ApprovalRuleset>
{
public ApprovalRulesetEntityConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasRequired(x => x.ApprovalRules)
.WithMany()
.HasForeignKey(x => x.Id);
}
}
Multiplicity 与 Role 中的引用约束冲突 关系中的“ApprovalRuleset_ApprovalRules_Target” 'ApprovalRuleset_ApprovalRules'。因为所有的属性在 依赖角色是不可为空的,主要角色的多重性 必须是“1”。
我错过了什么?我花了很长时间搜索堆栈溢出和谷歌。
【问题讨论】:
-
HasMany(x => x.ApprovalRules)是正确的配置,尽管按照惯例没有它的事件你应该得到你想要的。你能把ApprovalRule的配置也显示一下吗? -
事实证明,如果我有一个空白数据库,它使用 HasMany(x => x.ApprovalRules) 运行正常
标签: c# entity-framework fluent-entity-framework