使用带有泛型类的 ContextBuilder 的 Entity Framework 4 创建 EDMX/DB-Schema 时出现异常
我在 .NET 4 框架下使用 Microsoft.Data.Entity.CTP(在实体框架 CTP 中)从我的 C# 类创建 EDMX 元数据,以创建数据库架构。
我设置了一个简单的模型:
public class AModelContainer : ObjectContext
{
public IObjectSet<RegularClass> RegularClasses {
get { return CreateObjectSet<RegularClass>(); }
}
}
我遵循基于我的模型定义新 ContextBuilder 的简单模式。
var builder = new ContextBuilder<AModelContainer>();
using(var context = builder.Create(new SqlConnection(connString)))
{
context.RegularClasses.AddObject(new RegularClass());
context.SaveChanges();
}
这很好用。 直到我尝试做一些更复杂的事情...
我用通用类扩展我的模型
public class AModelContainer : ObjectContext
{
public IObjectSet<SpecialClass<string>> SpecialClasses {
get { return CreateObjectSet<SpecialClass<string>>(); }
}
}
现在在保存时我得到一个异常:
找不到 EntityType“Prototype.SpecialClass`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]”的映射和元数据信息。
在AModelContainer中的这一行:
return CreateObjectSet<SpecialClass<string>>();
我的通用“SpecialClass”的默认构造函数目前什么也不做,应该吗?
public class SpecialClass<T>
{
public SpecialClass()
{ }
}
或者这是 ContextBuilder 不知道到底要做什么的问题,有没有办法使用 builder.ComplexType() 或其他方法来指导它?
或者 CTP 还无法处理这种情况...
我的类名后面的“`1”在例外情况下也不适合我...
I'm using the Microsoft.Data.Entity.CTP (in the Entity Framework CTP) under the .NET 4 framework to create the EDMX metadata from my C# classes to create a database schema.
I setup a simple model as such:
public class AModelContainer : ObjectContext
{
public IObjectSet<RegularClass> RegularClasses {
get { return CreateObjectSet<RegularClass>(); }
}
}
I follow the simple pattern of defining a new ContextBuilder based on my model.
var builder = new ContextBuilder<AModelContainer>();
using(var context = builder.Create(new SqlConnection(connString)))
{
context.RegularClasses.AddObject(new RegularClass());
context.SaveChanges();
}
This works fine. Until I try to do something a little more complex...
I extend my model with a generic class
public class AModelContainer : ObjectContext
{
public IObjectSet<SpecialClass<string>> SpecialClasses {
get { return CreateObjectSet<SpecialClass<string>>(); }
}
}
Now on the save I get an exception:
Mapping and metadata information could not be found for EntityType 'Prototype.SpecialClass`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
On this line in the AModelContainer:
return CreateObjectSet<SpecialClass<string>>();
The default constructor of my generic 'SpecialClass' does nothing at the moment, should it?
public class SpecialClass<T>
{
public SpecialClass()
{ }
}
Or is this an issue with the ContextBuilder not knowing what to do exactly, is there a way to use builder.ComplexType(), or other method to guide it?
Or the CTP can't deal with this scenario yet...
That "`1" after my class name also doesn't sit well with me in the exception...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,您不能将泛型类用作 EF 的实体(以及扩展的仅代码)。
这是 CLR 和概念模型之间映射功能的限制。
在 Beta2 后,我们添加了允许此类事情的功能:
其中只有
Person
是 EF 模型中的“实体”,并且显示基类中的ID
属性作为Person
的属性。但 .NET 4.0 / EF 4 中没有计划支持将泛型类本身映射到模型中的实体。
希望这对
亚历克斯有帮助
You can't use Generic classes as Entities with the EF (and by extension Code-Only) today.
This is a limitation of the Mapping capabilities between the CLR and the Conceptual Model.
Post Beta2 we added the ability to allow this sort of thing:
Where only
Person
is an 'Entity' in the EF's model, and theID
property from the base class shows up as a property ofPerson
.But there are no plans in .NET 4.0 / EF 4 to support mapping a generic class itself to an Entity in the model.
Hope this helps
Alex