我的数据库中有以下表,所有表具有相同的表名但不同的模式 .
-
dbo.Versions
-
bpm.Versions
-
wf.Version
-
......
所有x.Versions都有一个FK到Version表 .
我已经从它创建了POCO类(这给了我类Version,Version1,.... - 我已经将classNames重命名为Version和BPMVersion,....但映射仍然存在于右表中 .
这是我映射到bpm.Versions的BPMVersion映射的示例
// Primary Key
this.HasKey(t => t.Id);
// Properties
// Table & Column Mappings
this.ToTable("Version", "bpm");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.VersionId).HasColumnName("VersionId");
this.Property(t => t.BPMId).HasColumnName("BPMId");
// Relationships
this.HasRequired(t => t.BPM)
.WithMany(t => t.BPMVersions)
.HasForeignKey(d => d.BPMId);
this.HasRequired(t => t.Version)
.WithMany(t => t.BPMVersions)
.HasForeignKey(d => d.VersionId);
创建迁移脚本时,我遇到以下异常: The entity types 'BPMVersion' and 'Version' cannot share table 'Versions' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.
我在互联网上找到了以下博客,它认为EF有同名但不同架构的表有问题(https://entityframework.codeplex.com/workitem/1641和http://geekswithblogs.net/tonyt/archive/2013/07/02/153327.aspx)
反正有没有重命名表名来避免这个问题?
1 回答
这与EF映射表名的方式有关 . 看起来这是EF中的一个错误 . 从EF 6.1开始,它仍处于建议状态,可悲地具有低优先级 .
详情请见:http://entityframework.codeplex.com/workitem/1641
Update :上面提到的EF WorkItem 1641已修复version 6.1.2