CodeFirst EF4.1 MVC 针对旧数据库 - 多重性冲突
无论我以哪种方式混合它,它都会给我带来错误。当我不断收到这些错误时,我有一种感觉,我错过了一些明显的东西。
在模型生成过程中检测到一个或多个验证错误:
System.Data.Edm.EdmAssociationType: : 多重性与关系“Venue_Courses”中角色“Venue_Courses_Source”中的引用约束冲突。由于从属角色中的所有属性均不可为 null,因此主体角色的重数必须为“1”。
System.Data.Edm.EdmAssociationEnd::多重性在关系“Venue_Courses”中的角色“Venue_Courses_Target”中无效。由于Dependent Role引用了关键属性,因此Dependent Role的重数上限必须为1。
一门课程只能有一个场地,场地可以被多个课程使用
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
希望这仍然能及时帮助您。我也遇到了完全相同的问题,并且困扰了近一个小时,直到我发现我的错误。
问题是
Course.Venue
关系是可选的(如 Fluent API 上声明的那样),但Course.VenueId
的 Id 声明是强制性的,因此您可以将 VenueId 设为通过将其更改为可选,或将关系更改为流畅 API 上的强制,一旦更改,OnModelCreating 应该可以正常运行。
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 ofCourse.VenueId
is mandatory, so you can either make VenueId optional by changing it toor change the relationship to mandatory on the fluent API, and the OnModelCreating should run fine once you changed that.
在网上搜索
System.Data.Edm.EdmAssociationType: : 多重性与角色中的引用约束冲突
后,它不断与这篇文章联系,所以这是我的问题和解决方案:
我将一个大型项目从 ef4.0 升级到 ef4.1使用 vs ef 逆向工程扩展。我们的 mvc 应用程序使用元数据类型和部分来装饰 ef4.0 对象。
删除元数据类型的文件后,项目开始工作。
根本问题是 [Required] 属性,因为 ef poco 对象可以为 null,而我的元数据类型在同一属性上具有 [Required]。以前是强制执行 mvc 验证规则,现在 ef4.1 用于填充导航属性。删除元数据类型中的 [Required] 解决了该问题。
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.
我在实体框架项目中遇到了这个错误,我通过更改 VenueId 的可为空值解决了该问题。
i 've struggled with this error in my entity framework project, i've solved the problem by changing nullable value of VenueId.
确保您不要在映射中将 HasKey() 与 HasOptional() 结合使用。在我的例子中,这导致了这个错误。
Make sure you don't use HasKey() in combination with HasOptional() in your mappings. That was causing this error in my case.