ADO实体框架帮助

时间:2009-12-23 21:27:55

标签: entity-framework

有一些Linq to SQL的经验我现在正在尝试ADO Entity框架。在Linq to SQL中,我将创建一个Linq to SQL类,将我的表拖过来构建数据上下文。然后我将实例化datacontext类并对datacontext类中的一个属性运行一些lambda。

现在,使用ADO实体框架,我添加了Entity Data Model类,并将表添加到数据模型中。我的实体数据模型类现在有一堆ObjectQuery<>属性,我添加的每个表都有一个。

现在我该如何处理这些属性?我怎么称呼他们?有人有代码示例吗?

1 个答案:

答案 0 :(得分:5)

不确定。我有a long presentation on this

作为对您的问题的简单回答,您可以使用ObjectQuery<T>属性执行以下操作。

返回对象列表:

IEnumerable<Customer> result = Context.Customers;
return result;

返回一个对象:

return Context.Customers.Where(c => c.Id == someId).First();

投影到演示模型:

return (from c in Customers
        where c.Id == someId
        select new CustomerPresentation
        {
            Id = c.Id,
            Name = c.Name,
            OrderCount = c.Orders.Count(),
            PhoneNumbers = from p in c.PhoneNumbers
                           select new PhoneNumberPresentation
                           {
                               AreaCode = p.AreaCode,
                               // etc.
                           },
            // etc.
        }).First();