好的,我在尝试指定连接表的名称时遇到了一些实体框架问题,如下所示:
// Used by a bunch of things...
public abstract BaseClass
{
public int Id { get; set; }
public string Name { get; set; }
}
public abstract ThingsBaseClass : BaseClass
{
public virtual ICollection<Things> Things { get; set; }
}
public First : ThingsBaseClass
{
// This holds items of type First and doesn't have any other properties
}
public Second : ThingsBaseClass
{
// This holds items of type Second and doesn't have any other properties
}
public Things
{
public int Id { get; set; }
public string Description { get; set; }
// Many to Many
public virtual ICollection<First> Firsts { get; set; }
public virtual ICollection<Second> Seconds { get; set; }
}
所以一切正常,除了表格如下:
First
FirstThings
Second
SecondThings
Things
我正在尝试重命名为:
First
Second
ThingFirsts
ThingSeconds
Things
尝试使用以下代码重命名它们会产生一些非常奇怪和随机的错误:
public class ThingConfiguration : EntityTypeConfiguration<Thing>
{
HasMany(x => x.Firsts)
.WithMany(x => x.Things)
.Map(x => x.ToTable("ThingFirsts"));
HasMany(x => x.Firsts)
.WithMany(x => x.Things)
.Map(x => x.ToTable("ThingSeconds"));
}
我正在尝试使用 Code First Migrations 来更新数据库(或者只是从头开始创建它)
错误包括以下一些:
Schema specified is not valid. Errors:
(28,6) : error 0040: Type Thing_First is not defined in namespace Project.Domain.Repositories (Alias=Self).
或者
Schema specified is not valid. Errors:
(126,6) : error 0074: NavigationProperty 'Thing' is not valid. Type 'Project.Domain.Repositories.Second' of FromRole 'Thing_Firsts_Target' in AssociationType 'Project.Domain.Repositories.Thing_Second' must exactly match with the type 'Project.Domain.Repositories.First' on which this NavigationProperty is declared on.
如果我摆脱 First 和 Second 的继承并直接放入,Id
它就可以正常工作。Name
ICollection<Thing> Things
没有理由使用继承,除了我有大约 5 个具有几乎相同的BaseClass
es 并希望使其保持干燥的对象。
我是否应该硬着头皮在任何地方重复代码以使其对实体框架更简单?
我错过了一些简单的东西吗?在使用继承类时我会遇到任何其他“问题”吗?
EF 6 对此有更好的支持吗?