Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

c# - CodeFirst EF4.1 MVC Against legacy database - Multiplicity conflicts

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
            
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...