使用带有泛型类的 ContextBuilder 的 Entity Framework 4 创建 EDMX/DB-Schema 时出现异常

发布于 2024-08-14 17:09:52 字数 1585 浏览 3 评论 0原文

我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2024-08-21 17:09:52

现在,您不能将泛型类用作 EF 的实体(以及扩展的仅代码)。

这是 CLR 和概念模型之间映射功能的限制。

在 Beta2 后,我们添加了允许此类事情的功能:

public class Entity<TKey>
{
   public TKey ID {get;set;}
}

public class Person: Entity<int>
{
   public string Firstname {get;set;}
   public string Surname {get;set;}
}

其中只有 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:

public class Entity<TKey>
{
   public TKey ID {get;set;}
}

public class Person: Entity<int>
{
   public string Firstname {get;set;}
   public string Surname {get;set;}
}

Where only Person is an 'Entity' in the EF's model, and the ID property from the base class shows up as a property of Person.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文