【问题标题】:Architecture with SQL Server CE使用 SQL Server CE 的体系结构
【发布时间】:2011-01-17 13:58:36
【问题描述】:

我必须构建一些小型独立应用程序,这些应用程序可以复制到 USB 设备并从那里开箱即用地运行。所以我想使用 WPF,它首先使用 EF 代码连接到 SQL Server CE 数据库。

我的问题是我应该使用什么架构。尽管这些应用程序是独立的,但我仍然希望将 UI 与域与数据分离,以实现层的清晰分离。但我也不想让它变得太复杂。

所以,我想要一个 UI 层 (WPF/MVVM),它使用底层域层(具有域逻辑的域对象)和存储库(首先使用 EF 代码)。

我的问题是:在这种情况下我应该使用什么模式来使 EF 工作?是否有一个示例可以演示如何在这种情况下实现 CRUD 操作?例如,我是否应该创建一个上下文并将其保持打开状态?或者我应该实现工作单元模式并在需要时将对象附加到其他上下文?

或者你会以完全不同的方式来做吗?

感谢您的建议!

【问题讨论】:

    标签: c# entity-framework sql-server-ce code-first


    【解决方案1】:

    EF 上下文应尽可能短地打开。最好在 using 语句中使用它。

    private static void ApplyItemUpdates(SalesOrderDetail originalItem,
        SalesOrderDetail updatedItem)
    {
        using (AdventureWorksEntities context =
            new AdventureWorksEntities())
        {
            context.SalesOrderDetails.Attach(updatedItem);
            // Check if the ID is 0, if it is the item is new. 
            // In this case we need to chage the state to Added.
            if (updatedItem.SalesOrderDetailID == 0)
            {
                // Because the ID is generated by the database we do not need to
                // set updatedItem.SalesOrderDetailID.
                context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
            }
            else
            {
                // If the SalesOrderDetailID is not 0, then the item is not new
                // and needs to be updated. Because we already added the 
                // updated object to the context we need to apply the original values.
                // If we attached originalItem to the context 
                // we would need to apply the current values:
                // context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
                // Applying current or original values, changes the state 
                // of the attached object to Modified.
                context.ApplyOriginalValues("SalesOrderDetails", originalItem);
            }
            context.SaveChanges();
        }
    }
    

    有一个叫做 Attach 的方法,它把实体附加到一个上下文中:

    private static void AttachRelatedObjects(
        ObjectContext currentContext,
        SalesOrderHeader detachedOrder,
        List<SalesOrderDetail> detachedItems)
    {
        // Attach the root detachedOrder object to the supplied context.
        currentContext.Attach(detachedOrder);
    
        // Attach each detachedItem to the context, and define each relationship
        // by attaching the attached SalesOrderDetail object to the EntityCollection on 
        // the SalesOrderDetail navigation property of the now attached detachedOrder.
        foreach (SalesOrderDetail item in detachedItems)
        {
            currentContext.Attach(item);
            detachedOrder.SalesOrderDetails.Attach(item);
        }
    }
    

    http://msdn.microsoft.com/en-us/library/bb896271.aspx

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 2014-06-02
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多