CodeFirst EF4.1 MVC Against legacy database - Multiplicity conflicts

C#asp.net Mvc-3Entity FrameworkEf Code-First

C# Problem Overview


No matter which way I mix it, it gives me errors. I have a feeling I'm missing something obvious as I keep getting these errors.

> One or more validation errors were detected during model generation: > > System.Data.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'Venue_Courses_Source' in relationship 'Venue_Courses'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. > > System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Venue_Courses_Target' in relationship 'Venue_Courses'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 1.

A Course can only have one venue, venues can be used by many Courses

public class Course
{
    [Key]
    public virtual int Id { get; set; }
    public string Title { get; set; }
    public DateTime StartDate { get; set; }
    public int VenueId { get; set; }
    
    public virtual Venue Venue { get; set; }
}

public class Venue
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    
    public virtual ICollection<Course> Courses { get; set; }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    #region Courses
    //Table Alias
    modelBuilder.Entity<Course>().ToTable("DBSCHEMA.TR_COURSES");
    //Keys
    modelBuilder.Entity<Course>().HasKey(c => c.Id);
    //Joins
    //Join to Venues
    modelBuilder.Entity<Course>().HasOptional(c => c.Venue);
        
    //Fields
    modelBuilder.Entity<Course>().Property(c => c.Id).HasColumnName("COURSE_ID");
    modelBuilder.Entity<Course>().Property(c => c.Title).HasColumnName("CR_TITLE");
    modelBuilder.Entity<Course>().Property(c => c.StartDate).HasColumnName("START_DATE");
    modelBuilder.Entity<Course>().Property(c => c.VenueId).HasColumnName("VENUE_ID");
    #endregion


    #region Venues
    //Table Alias
    modelBuilder.Entity<Venue>().ToTable("DBSCHEMA.VENUES");
    //Keys
    modelBuilder.Entity<Venue>().HasKey(v => v.Id);
    //Joins
    modelBuilder.Entity<Venue>().HasMany(venue => venue.Courses);
    //Fields
    modelBuilder.Entity<Venue>().Property(v => v.Id).HasColumnName("VENUE_ID");
    modelBuilder.Entity<Venue>().Property(v => v.Name).HasColumnName("VENUE_NAME");
    #endregion
            
}

C# Solutions


Solution 1 - C#

Hope this is still on time to help you. I was also having the exact same problem and was troubling with it for almost an hour until I could spot my mistake.

The problem is that Course.Venue relationship is optional (as declared on the fluent API), but the Id declaration of Course.VenueId is mandatory, so you can either make VenueId optional by changing it to

public int? VenueId { get; set;}

or change the relationship to mandatory on the fluent API, and the OnModelCreating should run fine once you changed that.

Solution 2 - C#

After searching the web for

System.Data.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role

It kept comming up with this post so here was my problem and solution:

I upgraded a large project from ef4.0 to ef4.1 using the vs ef reverse engineering extension. Our mvc app was using metadatatype and partials to decorate ef4.0 objects.

After removing the files of the metadatatype the project began working.

The root problem was [Required] attribute as ef poco object had nullable and my metadatatype had [Required] on the same property. Previously was to enforce mvc validation rules and now ef4.1 was using to populate navigation properties. Removing [Required] off metadatatype fixed the problem.

public partial class AgentAgency
{
	public long OID { get; set; }
	public long? AgentOID { get; set; }
	public long? AgencyOID { get; set; }
	public string ReinsuranceYear { get; set; }
	public virtual Agency Agency { get; set; }
	public virtual Agent Agent { get; set; }
}

public class AgentAgencyMetadata
{
	public Int64 OID { get; set; }

	[Required]
	public Int64 AgentOID { get; set; }

	[Required]
	public Int64 AgencyOID { get; set; }
}

Solution 3 - C#

i 've struggled with this error in my entity framework project, i've solved the problem by changing nullable value of VenueId.

Solution 4 - C#

Make sure you don't use HasKey() in combination with HasOptional() in your mappings. That was causing this error in my case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid CView Question on Stackoverflow
Solution 1 - C#Claiton LovatoView Answer on Stackoverflow
Solution 2 - C#Leblanc MenesesView Answer on Stackoverflow
Solution 3 - C#lomecView Answer on Stackoverflow
Solution 4 - C#ancajicView Answer on Stackoverflow