EF4 - 定制ObjectContext和继承问题

问题描述:

进一步转发了上一个问题。假设我从Post继承BlogEntry和Comment。我现在想定制他们一点。对博客文章的评论不需要标题,但评论需要用户参考,因此我将这两个字段从Post中移至Comment和Blog条目,如下所示:

Spinning further on the previous question I had. Let's say I inherit BlogEntry and Comment from Post. I now want to customize them a bit. A comment to a blog post does not need a title but a comment needs a user reference so I move these two fields out from Post and into Comment and Blog entry like this:

public abstract class Post
{
    public virtual int Id { get; set; }
    public virtual string Text { get; set; }
    public virtual DateTime CreatedAt { get; set; }
}

public class BlogEntry : Post
{
    public virtual string Header { get; set; }
    public virtual Blog Blog { get; set; }
    public virtual IEnumerable<Comment> Comments { get; set; }
}

public class Comment : Post
{
    public virtual string Header { get; set; }
    public virtual int UserId { get; set; }
    public virtual BlogEntry BlogEntry { get; set; }
}

现在我创建我的自定义对象上下文:

Now I create my custom object context:

public class EntityContext : System.Data.Objects.ObjectContext
{
    public EntityContext() : base("name=Entities", "Entities")
    {
        this.Blogs = CreateObjectSet<Blog>();
        this.Posts = CreateObjectSet<Post>();
        this.Entries = CreateObjectSet<BlogEntry>();
        this.Comments = CreateObjectSet<Comment>(); 
    }
    public ObjectSet<Blog> Blogs { get; set; }
    public ObjectSet<Post> Posts { get; set; }
    public ObjectSet<BlogEntry> Entries { get; set; }
    public ObjectSet<Comment> Comments { get; set; }
}

这给了我以下实际上非常描述性的错误信息:

This gives me the following actually quite descriptive error message:


测试方法抛出异常:
System.ArgumentException:有
否为
定义的EntitySets指定实体类型' BlogEntry。如果
'BlogEntry'是派生类型,请改用
基类型。例如,如果您调用
CreateObjectSet()
和DiscontinuedProduct是一个已知的
实体类型,但不直接将
映射到EntitySet,则
将会看到此错误。
DiscontinuedProduct类型可能是
派生类型,其父类型为
映射到EntitySet或
DiscontinuedProduct类型可能不是
全部映射到EntitySet 。
参数名称:TEntity

Test method threw exception: System.ArgumentException: There are no EntitySets defined for the specified entity type 'BlogEntry'. If 'BlogEntry' is a derived type, use the base type instead. For example, you would see this error if you called CreateObjectSet() and DiscontinuedProduct is a known entity type but is not directly mapped to an EntitySet. The DiscontinuedProduct type may be a derived type where the parent type is mapped to the EntitySet or the DiscontinuedProduct type might not be mapped to an EntitySet at all. Parameter name: TEntity

现在我不是继承和东西的主人,但我看到这样的方式是将一组条目和注释添加为ObjectSet<发布>这将解决我的问题?

Now I am not a master of inheritance and stuff but the way I see this would be to add a set of Entries and Comments as ObjectSet< Post> that would solve my problems?

如果您创建一个 ObjectSet 对于基类型(即 Post ),您不能为派生类创建一个类型,因为您可以从继承层次结构中检索所有类型的实例 ObjectSet

If you create an ObjectSet for a base type (i.e. Post) you can't create one for derived types too, because you can retrieve instances of all types in the inheritance hierarchy from that one ObjectSet.

ie ctx.Posts.OfType< BlogEntry>()将返回BlogEntry(s)。

i.e. ctx.Posts.OfType<BlogEntry>() would return BlogEntry(s).

所以答案是只需删除其他两套。

So the answer is to simply remove the other two sets.

如果你需要添加一个例子,你可以这样做:

If yo need to do an add for example you can do this:

ctx.Posts.AddObject(new BlogEntry {....});

根本没有问题。

帮助您更容易地编写查询,您可能会添加一些属性到您的ObjectContext,如下所示:

To help you write queries more easily you could probably add a couple of properties to your ObjectContext that look like this:

public ObjectQuery<BlogEntity> Blogs{
   get{
       return ctx.Posts.OfType<BlogEntry>() as ObjectQuery<BlogEntry>;
   }
} 

与评论相同。

希望这有助于

Alex