在EF代码中首先使用连接的POCO而不是断开的POCO进行CRUD操作 [英] CRUD operations with connected POCOs instead of disconnected POCOs in EF code first

查看:149
本文介绍了在EF代码中首先使用连接的POCO而不是断开的POCO进行CRUD操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经典的3层项目,具有以下层:

I have an classic 3 layer project, that has these layers:


  • UI(WinForm项目)

  • BLL

  • DAL(使用EF codefirst)

我的实体已断开连接 POCO s,我用它们在层之间传递数据,并使用它们绑定到UI控件。

My entities are disconnected POCOs and I used them to pass data between layers and used them to bind to the UI controls.

我加载一些相关实体到UI的图表(例如 Order ,它的 OrderLine s和 Product 每个 OrderLine ),用户可以添加一些新实体,编辑其他实体,并删除该图中的其他实体,然后将此图发送到DAL将此更改保留到DB。

I load a graph of some related entities to UI(e.g. Order with it's OrderLines and Product of each OrderLine), user may add some new entities, edit some others and delete some others entities in this graph, and then send this graph to DAL to persist this changes to DB.

我想要所有这些CRUD操作适用于一个事务。所以,对于这个要求,由于我的 POCO 被断开连接,我使用以下状态 enum 为我的 BaseEntity 类保持其状态在客户端(所有其他实体继承自 BaseEntity ):

I want to All these CRUD operations apply in one transaction. so, for this requirement, and because my POCOs are disconnected, I used following State enum for my BaseEntity class to keep its state in client side(all other entities inherited from BaseEntity):

public enum States
    {
        Unchanged,
        Added,
        Modified,
        Deleted
    }

在DAL中,当我想坚持这个改变的对象图,因为这个实体图与DbContext断开连接,所以我应该把图的根添加到创建的DbContext中,因为在将根附加到 DbContext DbContext 中的所有条目将为添加,我应该同步 State 状态的DbContext中的每个条目的c>,所以我使用这些方法来做到这一点:

In DAL, when I want to persist this changed graph of objects, because this entity graph is disconnected from DbContext, I should attach root of my graph to the created DbContext and because after attaching the root to DbContext state of all entries in the DbContext will be Added, I should sync State of each Entry in DbContext with State of entities in my graph, so I used these methods to do this:

public static EntityState ConvertState(BaseEntity.States state)
{
    switch (state)
    {
        case BaseEntity.States.Added:
            return EntityState.Added;
        case BaseEntity.States.Modified:
            return EntityState.Modified;
        case BaseEntity.States.Deleted:
            return EntityState.Deleted;
        default:
            return EntityState.Unchanged;
    }
}

public void ApplyChanges<TEntity>(TEntity root) where TEntity : BaseEntity
{
   _dbContext.Set<TEntity>().Add(root);
    foreach (var entry in _dbContext.ChangeTracker
    .Entries<BaseEntity>())
    {
        BaseEntity stateInfo = entry.Entity;
        entry.State = ConvertState(stateInfo.State);
    }
}

如你所见,我做了很多东西在EF之外,不要使用EF功能,因为我的POCO断开连接。

As you see, I am doing a lot of stuff outside of EF and do not use EF features, because my POCOs are disconnected.

有没有办法应用这些批量CRUD情景,而不会断开我的 POCO s from DbContext

Is there any way to apply these bulk CRUD scenario, without disconnecting my POCOs from DbContext?

推荐答案

对于断开连接的POCO实体,我坚持遵循这种模式。

For disconnected POCO entities I have followed this pattern when persisting.


  • 从数据库获取连接的实体图。

  • 将断开连接的图形映射到已连接的图形。

  • 持续连接的图形

目前,您正在尝试通过UI跟踪更改,当这些更改可能或可能与您准备坚持的时间相关时,这是最麻烦的。我发现,在持久化之前从数据库获取最新的实体可以做得更好。

Currently you are attempting to track changes all the way up through the UI, when these changes may or may not be relevant by the time you are ready to persist, which is cumbersome at best. I have found that getting the most recent entity from the db just prior to persisting works out much better.

这也使您能够在执行时检查并发问题映射。

This also gives you the ability to check for concurrency issues when performing the mapping.

这篇关于在EF代码中首先使用连接的POCO而不是断开的POCO进行CRUD操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆