简体   繁体   English

如何在Entity Framework 6中使用Fluent API映射没有外键属性的一对多对象列表?

[英]How can I use Fluent API in Entity Framework 6 to Map a List of One To Many objects without a foreign key property?

I have the following classes (that i'm not able to update, add properties or add annotations to): 我有以下类(无法更新,添加属性或添加注释):

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

I'm trying to write some Fluent API code with Entity Framework 6 to map these to two tables. 我正在尝试使用Entity Framework 6编写一些Fluent API代码,以将它们映射到两个表。

This is the ApprovalRule configuration: 这是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);
    }
}

So far I've got: 到目前为止,我已经:

public class ApprovalRulesetEntityConfiguration : EntityTypeConfiguration<ApprovalRuleset>
{
    public ApprovalRulesetEntityConfiguration()
    {
        HasKey(x => x.Id);
        Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        HasMany(x => x.ApprovalRules);
    }
}

The Foreign Key on table 'ApprovalRules' with columns 'ApprovalRuleset_Id' could not be created because the principal key columns could not be determined. 由于无法确定主键列,因此无法在表'ApprovalRules'上创建带有列'ApprovalRuleset_Id'的外键。 Use the AddForeignKey fluent API to fully specify the Foreign Key. 使用AddForeignKey流利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 conflicts with the referential constraint in Role 'ApprovalRuleset_ApprovalRules_Target' in relationship 'ApprovalRuleset_ApprovalRules'. 多重性与关系“ ApprovalRuleset_ApprovalRules”中的角色“ ApprovalRuleset_ApprovalRules_Target”中的引用约束冲突。 Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. 因为从属角色中的所有属性都是不可为空的,所以主体角色的多重性必须为'1'。

What am I missing? 我想念什么? I've spent ages searching stack overflow and google. 我花了很长时间搜索堆栈溢出和谷歌。

You will need to add two properties in ApprovalRule class. 您将需要在ApprovalRule类中添加两个属性。 Then mapping would be like this - 然后映射将是这样的-

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

    public int ApprovalRulesetId { get; set; }
    public virtual ApprovalRuleset ApprovalRuleset { get; set; }
}

public class ApprovalRulesetEntityConfiguration : EntityTypeConfiguration<ApprovalRuleset>
{
    public ApprovalRulesetEntityConfiguration()
    {
        HasKey(t => t.Id);
    }
}

public class ApprovalRuleEntityConfiguration : EntityTypeConfiguration<ApprovalRule>
{
    public ApprovalRuleEntityConfiguration()
    {
        HasKey(t => t.Id);

        /*Property(t => t.Value)
            .HasMaxLength(100);*/

        HasRequired(t => t.ApprovalRuleset)
            .WithMany(t => t.ApprovalRules)
            .HasForeignKey(d => d.ApprovalRulesetId);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM