简体   繁体   English

持续使用实体框架在WPF中进行的更改

[英]Persisting Changes Made in WPF with Entity Framework

I'm having an issue where Entity Framework isn't persisting changes to the DB. 我遇到了一个问题,即实体框架无法持久保存对数据库的更改。 I use a static method to load up some values and the Entity context is thrown away (in a using statement). 我使用静态方法加载一些值,并且Entity上下文被丢弃(在using语句中)。

The objects are then loaded into a WPF DataGrid where they can be manipulated by the end user. 然后将对象加载到WPF DataGrid中,最终用户可以在其中操作它们。

When the user is finished making changes an "update" button is pressed and the list of objects is sent back to the data layer to be persisted to the DB. 当用户完成更改后,将按下“更新”按钮,并将对象列表发送回数据层,以保留到数据库中。 I can see objects that were changed in the UI reflect their new value (ie not a data binding issue). 我可以看到在UI中更改的对象反映了它们的新值(即,不是数据绑定问题)。

I would assume since the entity context that loaded the objects has since been disposed of I should then attach these objects to be persisted to the newly created context. 我假设由于加载对象的实体上下文已经被丢弃,因此我应该将这些对象附加到新创建的上下文中。 Correct? 正确? When I do this the entity state of the modified object (I can see that is what the state is set to) changes to "Unchanged". 当我这样做时,修改后的对象的实体状态(我可以看到状态设置为该状态)更改为“未更改”。 Nothing is persisted to the DB. 什么都不会保留到数据库。

What the heck am I missing?! 我到底想念什么?

Here is the code to load and update the values: 这是加载和更新值的代码:

public static List<SSIS_Configuration> GetConfigurationValuesForTenant(string tenantKey, SqlConnectionString connectionString)
    {
        List<SSIS_Configuration> configStrings = new List<SSIS_Configuration>();
        if (connectionString.IsValid())
        {
            try
            {

                using (SSISFrameworkEntities entities = new SSISFrameworkEntities(connectionString.ToEDMXString("SSISFramework.SSISFramework")))
                {
                    string configFilterStartingValue = "CommonConfig_" + tenantKey;

                    configStrings = (from configString in entities.SSIS_Configurations
                                    where configString.ConfigurationFilter.StartsWith(configFilterStartingValue)
                                    select configString).ToList();
                }
            }
            catch { }
        }

        return configStrings;
    }

    public static void UpdateConfigurations(List<SSIS_Configuration> configurations, SqlConnectionString connectionString)
    {
        if (connectionString.IsValid())
        {
            try
            {
                using (SSISFrameworkEntities entities = new SSISFrameworkEntities(connectionString.ToEDMXString("SSISFramework.SSISFramework")))
                {
                    foreach (SSIS_Configuration config in configurations)
                    {
                        entities.Attach(config);
                    }

                    entities.SaveChanges();
                }
            }
            catch { }
        }
    }

WPF application is scenario for attached entities so you should not dispose context if you are going to change entities loaded from the context. WPF应用程序是附加实体的方案,因此,如果要更改从上下文加载的实体,则不应处置上下文。 If you dispose it you must implement a lot of additional logic because you must tell a new context about every single change a user did to the entities and relations. 如果要处置它,则必须实现许多其他逻辑,因为必须将用户对实体和关系所做的每项更改告知新的上下文。

So in your scenario calling Attach will only connects entities to the context but you need to set also their state (attaching put entities into Unchanged state). 因此,在您的方案中,调用Attach将仅将实体连接到上下文,但是您还需要设置它们的状态 (将放置的实体附加为Unchanged状态)。 Be aware that you must set state correctly to Modified , Deleted or Inserted based on the operation you want to perform. 请注意,必须根据要执行的操作将状态正确设置为“ Deleted Modified ,“ Deleted或“ Inserted If your changed entities have some relations which have been also changes you must set state for related entities as well and in case of changed relation between entities you must change state of relations itself as well. 如果您更改的实体具有某些也已更改的关系,则还必须为相关实体设置状态,如果实体之间的关系发生更改,则也必须更改关系本身的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM