CodeFirst EF4.1 MVC Versus Old Database - Multiplicity Conflicts

No matter how I mix it, it gives me errors. I have the feeling that I'm missing something obvious as I keep getting these errors.

During model generation, one or more validation errors were detected:

System.Data.Edm.EdmAssociationType :: Multiplicity conflicts with a relational constraint in the role of "Venue_Courses_Source" in relation to "Venue_Courses". Since all properties of the dependent role are not NULL, the multiplicity of the leading role must be "1".

System.Data.Edm.EdmAssociationEnd :: multiplicity is not valid in the role of "Venue_Courses_Target" in relation to "Venue_Courses". Since the dependent role refers to key properties, the upper bound on the plurality of the dependent role must be 1.

A course can consist of only one place, places 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 } 
+53
c # asp.net-mvc-3 entity-framework ef-code-first
Nov 04 2018-11-11T00:
source share
4 answers

Hope this is still on time to help you. I also had the same problem, and I was worried about it for almost an hour, until I could detect my mistake.

The problem is that the Course.Venue relation is optional (as indicated in the free API), but the Id Course.VenueId is required, so you can make the VenueId parameter optional by changing it to

 public int? VenueId { get; set;} 

or change the attitude towards the required free API, and OnModelCreating should work fine after you change this.

+139
Nov 09 '11 at 17:11
source share

After searching the web for

System.Data.Edm.EdmAssociationType :: Multiplicity conflicts with a relational constraint in a role

He continued to follow this post, so here is my problem and solution:

I upgraded a large project from ef4.0 to ef4.1 using the vs ef reverse engineering extension. Our mvc application used metadata and partial parts to decorate ef4.0 objects.

After deleting the metadata files, the project started to work.

The root problem was the [Required] attribute, because the ef poco object was NULL, and my metadata was [Required] in the same property. There used to be compliance with the mvc validation rules, and now ef4.1 used to populate the 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; } } 
+4
Jan 26 '12 at 6:15
source share

I struggled with this error in my entity framework project, I solved the problem by changing the NULL value assigned to the value.

+2
01 Oct '12 at 11:05
source share

Make sure you are not using HasKey () in combination with HasOptional () in your mappings. This caused this error in my case.

+2
Jan 17 '14 at 13:10
source share



All Articles