如何查询上下文,以便创建子类POCO实体?

时间:2012-07-21 18:17:09

标签: c# entity-framework poco

说我有以下生成的Entity Framework POCO类:

public partial class Foo
{
    #region Primitive Properties

    public virtual long Id { get; set; }

    #endregion
}

我将其子类化:

public class SubclassedFoo : Foo
{
    public override long Id
    {
        get { return base.Id; }
        set { base.Id = value; }
    }
}

ObjectContext如何检索SubclassedFoo个对象而不是Foo个对象?

例如,我可以用什么代替呢?

ObservableCollection<Foo> foos = context.Foos

如:

ObservableCollection<SubclassedFoo> subclassedFoos = context.???

1 个答案:

答案 0 :(得分:1)

使用OfType<SubclassedFoo>过滤:

List<SubclassedFoo> subclassedFoos = context.Foos
    .OfType<SubclassedFoo>()
    .ToList();

或者如果你真的想要一个ObservableCollection

ObservableCollection<SubclassedFoo> subclassedFoos =
    new ObservableCollection<SubclassedFoo>(context.Foos
        .OfType<SubclassedFoo>()
        .AsEnumerable());