codeFirst EF4.1 MVC对原有数据库 - 多重冲突 [英] CodeFirst EF4.1 MVC Against legacy database - Multiplicity conflicts

查看:561
本文介绍了codeFirst EF4.1 MVC对原有数据库 - 多重冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有哪种方式我混了问题,它给我的错误。我有一种感觉,我失去了一些东西明显,因为我不断收到这些错误。


  

模型生成过程中检测到一个或多个验证错误:


  
  

System.Data.Edm.EdmAssociationType:在关系中的Venue_Courses角色Venue_Courses_Source引用约束多重冲突。因为所有的从属角色的属性是不可为空,校长角色的多重性必须是'1'。


  
  在关系'Venue_Courses多重不在角色有效的Venue_Courses_Target':

System.Data.Edm.EdmAssociationEnd。因为从属作用是指关键性质,上限的从属地位的多重性必须是1


教程只能有一个场地,场地能够被许多课程使用

 公共类课程
{
    [键]
    公共虚拟INT标识{搞定;组; }
    公共字符串名称{搞定;组; }
    公开日期时间起始日期{搞定;组; }
    公众诠释VenueId {搞定;组; }    公共虚拟会场地点{搞定;组; }
}公共类地点
{
    [键]
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }    公共虚拟的ICollection<课程和GT;课程{搞定;组; }
}
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{    #区域课程
    //表的别名
    modelBuilder.Entity<课程和GT;()ToTable(DBSCHEMA.TR_COURSES);
    //键
    modelBuilder.Entity<课程和GT;()HasKey(C => c.Id)。
    //加入
    //加入到场地
    modelBuilder.Entity<课程和GT;()HasOptional(C => c.Venue)。    //领域
    modelBuilder.Entity<课程和GT;()房产(C => c.Id)。.HasColumnName(COURSE_ID);
    modelBuilder.Entity<课程和GT;()房产(C => c.Title)。.HasColumnName(CR_TITLE);
    modelBuilder.Entity<课程和GT;()房产(C => c.StartDate)。.HasColumnName(START_DATE);
    modelBuilder.Entity<课程和GT;()房产(C => c.VenueId)。.HasColumnName(VENUE_ID);
    #endregion
    #区域场地
    //表的别名
    modelBuilder.Entity<地点及GT;()ToTable(DBSCHEMA.VENUES);
    //键
    modelBuilder.Entity<地点及GT;()HasKey(V => v.Id)。
    //加入
    modelBuilder.Entity<地点及GT;()的hasMany(场地=> venue.Courses)。
    //领域
    modelBuilder.Entity<地点及GT;()房产(V => v.Id)。.HasColumnName(VENUE_ID);
    modelBuilder.Entity<地点及GT;()房产(V => v.Name)。.HasColumnName(VENUE_NAME);
    #endregion}


解决方案

希望这是仍然时间来帮助你。我也有完全相同的问题,并与它困扰了将近一个小时,直到我能发现我的错误。

问题是, Course.Venue 的关系是可选的(在流畅的API作为申报),但对标识声明 Course.VenueId 是强制性的,所以你可以让VenueId可选,改成

 公众诠释? VenueId {搞定;组;}

或更改的流畅API的关系强制性,一旦你改变了OnModelCreating应该运行正常。

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

}

解决方案

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.

这篇关于codeFirst EF4.1 MVC对原有数据库 - 多重冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆