开发者

Persisting Changes Made in WPF with Entity Framework

开发者 https://www.devze.com 2023-04-01 02:02 出处:网络
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).

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).

The objects are then loaded into a WPF DataGrid where they can be manipulated by the end user.

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).

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". N开发者_开发知识库othing 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. 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). Be aware that you must set state correctly to Modified, Deleted or Inserted based on the operation you want to perform. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消