简体   繁体   中英

Entity framework fluent mapping many-to-may

I'm having a problem when I try to map this model:

public class Discipline
{
    public int Id {get;set;}
    public string Name{get;set;}
    public virtual ICollection<DisciplineRequirement> Requirements {get;set;}
}

public class DisciplineRequirement
{
    public int DisciplineId {get;set;}
    public int RequiredDiscipline {get;set;}
    public virtual Discipline Discipline {get;set;}
    public virtual Discipline RequiredDiscipline {get;set;}
}

Mapping code:

public class DisciplineMap : EntityTypeConfiguration<Discipline>
{
    public DisciplineMap()
    {
        ToTable("Discipline");

        HasKey(p => p.Id);

        HasMany(p => p.Requirements)
            .WithRequired(p => p.Discipline)
            .HasForeignKey(p => p.DisciplineId);

        HasMany(p => p.Requirements)
            .WithRequired(p => p.RequiredDiscipline)
            .HasForeignKey(p => p.RequiredDisciplineId);
    }
}

public class DisciplineRequirementMap : EntityTypeConfiguration<DisciplineRequeriment>
{
    public DisciplineRequirementMap()
    {
        ToTable("DisciplineRequirement");

        HasKey(p => new
        {
            p.DisciplineId,
            p.RequiredDisciplineId
        });

        HasRequired(p => p.Discipline)
            .WithMany(p => p.Requirements)
            .HasForeignKey(p => p.DisciplineId);

        HasRequired(p => p.RequiredDiscipline)
            .WithMany(p => p.Requirements)
            .HasForeignKey(p => p.RequiredDisciplineId);

    }
}

I've got this error:

One or more validation errors were detected during model generation: Requirements: FromRole: NavigationProperty 'Requirements' is not valid. Type 'DisciplineRequeriment' of FromRole 'Discipline_Requirements_Target' in AssociationType 'Discipline_Requirements' must exactly match with the type 'Discipline' on which this NavigationProperty is declared on.

Can anyone give me a light here?

imho, you must have two navigation properties (or at least if just one you can't configure it with two FKs)

public class Discipline
{
    public int Id {get;set;}
    public string Name{get;set;}
    public virtual ICollection<DisciplineRequirement> Requirements {get;set;}
    public virtual ICollection<DisciplineRequirement> RequiredBy {get;set;}
}

public class DisciplineRequirement
{
    public int DisciplineId {get;set;}
    public int RequiredDisciplineId {get;set;}
    public virtual Discipline Discipline {get;set;}
    public virtual Discipline RequiredDiscipline {get;set;}
}

with

public class DisciplineMap : EntityTypeConfiguration<Discipline>
{
    public DisciplineMap()
    {
        ToTable("Discipline");

        HasKey(p => p.Id);
    }
}

public class DisciplineRequirementMap : EntityTypeConfiguration<DisciplineRequeriment>
{
    public DisciplineRequirementMap()
    {
        ToTable("DisciplineRequirement");

        HasKey(p => new
        {
            p.DisciplineId,
            p.RequiredDisciplineId
        });

        HasRequired(p => p.Discipline)
            .WithMany(p => p.RequiredBy)
            .HasForeignKey(p => p.DisciplineId);

        HasRequired(p => p.RequiredDiscipline)
            .WithMany(p => p.Requirements)
            .HasForeignKey(p => p.RequiredDisciplineId);

    }
}

should be enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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