【发布时间】:2013-03-21 21:37:08
【问题描述】:
我有几个相同的实体,除了每个都映射到相应的相同表的类名。每个表的映射类似于以下内容:
modelBuilder.Entity<Foo>().Map(x =>
{
x.MapInheritedProperties();
x.ToTable("Foo");
})
这种方法有效,但重复。
我创建了这个类,希望摆脱重新定位。为简洁起见,此处进行了简化。
public class Generic<T>
{
public Generic(DbModelBuilder modelBuilder, string tableName)
{
modelBuilder.Entity<T>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable(tableName);
});
}
}
我收到以下我不理解的编译器错误:
The type 'T' must be a reference type in order to use it as parameter 'TEntityType' in the generic type or method 'System.Data.Entity.DbModelBuilder.Entity<TEntityType>()'
- 与许多 .Net 编码人员一样,我经常使用泛型,但不经常编写它们。
- 我使用 EF 有一段时间了,但我对 Code First 还是很陌生
- 我在 SO 上下进行了很多搜索,但都没有运气。
- 我做错了什么?我有什么不明白的?
提前致谢, 吉姆
【问题讨论】:
标签: entity-framework ef-code-first entity-framework-5