C# 基于sqlserverce的体系结构

C# 基于sqlserverce的体系结构,c#,entity-framework,sql-server-ce,code-first,C#,Entity Framework,Sql Server Ce,Code First,我必须构建一些小型的独立应用程序,这些应用程序可以复制到USB设备,并从那里开箱即用地运行。所以我想使用WPF,它首先使用EF代码连接到SQLServerCE数据库 我的问题是我应该使用什么架构。虽然这些应用程序是独立的,但我仍然希望将UI从域和数据中分离出来,以实现清晰的层分离。但我也不想让它太复杂 因此,我希望有一个UI层(WPF/MVVM),它使用底层域层(具有域逻辑的域对象)和存储库(首先使用EF代码) 我的问题是:在这种情况下,我应该使用什么模式使EF工作?是否有一个示例演示了如何在这

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

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

因此,我希望有一个UI层(WPF/MVVM),它使用底层域层(具有域逻辑的域对象)和存储库(首先使用EF代码)

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

或者你会用完全不同的方式来做


谢谢你的建议

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);
    }
}
private static void attacherRelatedObject(
ObjectContext当前上下文,
SalesOrderHeader detachedOrder,
列表(EMS)
{
//将根detachedOrder对象附加到提供的上下文。
currentContext.Attach(detachedOrder);
//将每个detachedItem附加到上下文,并定义每个关系
//通过将附加的SalesOrderDetail对象附加到上的EntityCollection
//现在连接的detachedOrder的SalesOrderDetail导航属性。
foreach(detachedItems中的SalesOrderDetail项)
{
currentContext.Attach(项目);
detachedOrder.SalesOrderDetails.Attach(项目);
}
}