将 EntityFramework 4 实体公开为 IList 而不是 IObjectSet
我的 Entity Framework 4 项目中有一个“客户”POCO 实体。我想将我的客户实体作为通用列表而不是对象集公开给我的上层。
我有一个 IUnitOfWork 接口,如下所示:
public interface IUnitOfWork
{
string Save();
IList<Customer> Customers { get; }
}
在我的实体框架 DAL(实现上述接口)下,我有以下内容:
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>() ;
}
}
}
但是 'CreateObjectSet("Customers")' 行不起作用。每次我尝试添加新的“客户”时,都没有发生任何事情。有趣的是,如果我恢复使用 IObjectSet 那么代码就可以工作。例如:
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 也可以工作,但我无法让 IList 工作,我不知道为什么。有人有什么想法吗?
#
对原始问题的更正。使用 IQueryable 不起作用,IEnumerable 也不起作用。这是因为客户存储库需要提供“添加”和“删除”方法来从实体集合中添加/删除(在上面的示例中添加或删除客户实体)。 IQueryable 或 IEnumerable 都不允许您添加或删除对象;相反,必须使用 ICollection 或 IList。这让我回到了原来的问题。我不想将我的集合作为对象集公开到存储库。我想使用不依赖于 EntityFramework 的类型,即 - 我想使用通用列表。
还有人有更多建议吗?我怀疑有一种简单的方法可以做到这一点,但我对框架不够熟悉,无法弄清楚。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎在所有这一切中都缺少一个存储库。存储库通常负责处理从
ObjectSet
到IList
的转换(或者,在大多数情况下,IEnumerable
或IQueryable
)。然后,我通常让我的工作单元创建应在工作单元中登记的存储库,以便通过存储库完成的任何操作都捆绑在单个操作中。
请记住,我的 UnitOfWork 只有两种方法。一个用于获取存储库,另一个用于提交工作单元。所有数据检索均由存储库处理。
You seem to be missing a Repository in all of this. The Repository is usually what handles the conversion from
ObjectSet<T>
toIList<T>
(or, in most cases,IEnumerable<T>
orIQueryable<T>
).I usually then have my UnitOfWork create the Repositories that should be enlisted in the Unit of Work so that anything done through the repositories is bundled in a single operation.
Keep in mind, that my UnitOfWork only would have two methods. One for getting a repository and another for committing the Unit of Work. All data retrieval is handled by the Repositories.
_customers.ToList() 是罪魁祸首。 ToList 执行查询并将该查询中的所有项目复制到新的集合对象中。这个新的集合对象不提供 ObjectSet 所具有的跟踪功能。
_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.