将EntityFramework 4实体暴露为IList而不是IObjectSet [英] Exposing EntityFramework 4 entities as IList instead of IObjectSet
问题描述
我有一个IUnitOfWork界面,如下所示:
public interface IUnitOfWork
{
string Save();
IList< Customer>客户{get;
}
在我的实体框架下DAL(实现上述界面)以下内容:
public class EntityContainer:ObjectContext,IUnitOfWork
{
private IObjectSet< Customer> _顾客;
public IList< Customer>客户
{
get
{
if(_customers == null)
{
_customers = CreateObjectSet< Customer>(Customers);
}
return _customers.ToList< Customer>();
}
}
}
然而'CreateObjectSet客户)'行不行。每次我尝试添加一个新的客户什么都不发生。有趣的是,如果我恢复使用IObjectSet,那么代码可以工作。例如:
public interface IUnitOfWork
{
string Save();
IObjectSet< Contact>联系人{get; }
}
public class EntityContainer:ObjectContext,IUnitOfWork
{
private IObjectSet< Customer> _顾客;
public IObjectSet< Customer>客户
{
get
{
if(_customers == null)
{
_customers = CreateObjectSet< Customer>(Customers);
}
return _customers;
}
}
}
IQueryable也可以,但是不能让IList工作,我不知道为什么。任何人有任何想法?
#
更正原始问题。使用IQueryable不起作用,IEnumerable也不起作用。这是因为客户存储库需要提供添加和删除方法来添加/删除实体集合(在上面的示例中添加或删除客户实体)。 IQueryable或IEnumerable都不允许您添加或删除对象;相反,必须使用ICollection或IList。这让我回到原来的问题。我不想将我的集合作为ObjectSet暴露给存储库。我想使用一种不绑定到EntityFramework的类型,即我想使用通用列表。
有没有人有任何建议?我怀疑这样做有一个简单的方法,但是我对这个框架不够熟悉。
_customers.ToList()是罪魁祸首。 ToList执行查询并将该查询中的所有项目复制到新的集合对象中。此新的集合对象不提供ObjectSet具有的跟踪功能。
I have a 'Customer' POCO entity within my Entity Framework 4 project. I want to expose my Customer entities to my upper layers as a generic list rather than an ObjectSet.
I have an IUnitOfWork interface which looks as follows:
public interface IUnitOfWork
{
string Save();
IList<Customer> Customers { get; }
}
Down at my Entity Framework DAL (which implements the above interface) I have the following:
public class EntityContainer : ObjectContext, IUnitOfWork
{
private IObjectSet<Customer> _customers;
public IList<Customer> Customers
{
get
{
if (_customers == null)
{
_customers = CreateObjectSet<Customer>("Customers");
}
return _customers.ToList<Customer>() ;
}
}
}
However the 'CreateObjectSet("Customers")' line doesn't work. Every time I try to add a new 'Customer' nothing happens. Interestingly, if I revert to using an IObjectSet then the code works. For example:
public interface IUnitOfWork
{
string Save();
IObjectSet<Contact> Contacts { get; }
}
public class EntityContainer : ObjectContext, IUnitOfWork
{
private IObjectSet<Customer> _customers;
public IObjectSet<Customer> Customers
{
get
{
if (_customers == null)
{
_customers = CreateObjectSet<Customer>("Customers");
}
return _customers;
}
}
}
IQueryable also works, but I cannot get IList to work and I have no idea why. Anyone any ideas?
#
A correction to the original question. Using IQueryable doesn't work, nor does IEnumerable. This is because the Customer repository needs to provide 'Add' and 'Delete' methods to add/delete from the entity collection (add or remove customer entities in the above example). Neither IQueryable or IEnumerable allow you to add or remove objects; instead, an ICollection or IList must be used. This leaves me back at my original problem. I do not want to expose my collection to the repository as an ObjectSet. I want to use a type which is not tied to the EntityFramework i.e. - I want to use a generic list.
Has anyone any more suggestions? I suspect there's a straightforward way of doing this, but I'm not familiar enough with the framework to figure it out.
_customers.ToList() is the culprit. ToList executes the query and copies all the items from that query into a new collection object. this new collection object does not provide the tracking capabilities that ObjectSet has.
这篇关于将EntityFramework 4实体暴露为IList而不是IObjectSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!