我有几个实例除了类名称都相同,每个实例都映射到对应的相同表。每个表的映射类似于以下内容:无法使用泛型创建EF代码第一个映射类
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没有运气。
- 我在做什么错?我不明白什么?
由于提前, 吉姆
感谢您提供了很好的答案和额外的信息。我已经将早先的答复标记为答案。但是,我想让你知道我赞赏其他信息。我总是很欣赏学习新东西。 – 2013-03-21 22:08:13
@JimReineri欢迎您。 – 2013-03-22 09:25:15