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.

This is the ApprovalRule configuration:

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. Use the AddForeignKey fluent API to fully specify the Foreign Key.

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'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

What am I missing? I've spent ages searching stack overflow and google.

1

There are 1 best solutions below

2
On

You will need to add two properties in ApprovalRule class. 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);
    }
}