理解实体模型的刷新模式RefreshMode.ClientWins

如何影响本地实体


当实体状态(EntityStatus)为Unchanged时,与StoreWins相同,从数据源刷新实体。

当实体状态(EntityStatus)为Modified时,实体保持不变。


如何影响数据源更新


不使用ClientWins刷新,在调用SaveChanges时,实体按需更新,即实体属性状态为Modified才更新到数据源。

使用ClientWins刷新,调用SaveChanges时分两种情况:

当实体状态为Unchanged时不更新数据源。

当实体状态为Modified时,将本地实体更新到数据源,即使实体属性状态为Unchanged也将更新到数据源。


总结:实体未改变时以数据源为准,否则以本地实体为准。

 

测试代码:

 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Threading;
using System.Data.SqlClient;

namespace ContextRefreshDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (testEntities context = new testEntities())
            {
                person one = context.person.First(p => p.id == 1);
                one.age = 30;
                context.SaveChanges();
                Console.WriteLine("/r/n初始值/r/n编号:{0} 姓名:{1} 年龄:{2} 实体状态:{3}", one.id, one.name, one.age, one.EntityState.ToString());

                using (SqlCommand cmd = new SqlCommand("update person set age=60 where id=1", new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True;")))
                {
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
                Console.WriteLine("/r/n通过ADO修改年龄为60,当前模型值未改变:/r/n编号:{0} 姓名:{1} 年龄:{2} 实体状态:{3}", one.id, one.name, one.age, one.EntityState.ToString());

                context.SaveChanges();
                Console.WriteLine("/r/n调用SaveChanges后,因为实体状态未改变,并不会更新数据库:/r/n编号:{0} 姓名:{1} 年龄:{2}", one.id, one.name, one.age);

                //注意:下面这句会影响Age是否被更新
                //one.name = one.name;
                context.Refresh(RefreshMode.ClientWins, one);
                Console.WriteLine("/r/n调用RefreshMode.ClientWins后:/r/n编号:{0} 姓名:{1} 年龄:{2} 实体状态:{3}", one.id, one.name, one.age, one.EntityState.ToString());

                context.SaveChanges();
                Console.WriteLine("/r/n调用SaveChanges后,从当前实体更新数据库:/r/n编号:{0} 姓名:{1} 年龄:{2}", one.id, one.name, one.age);

                context.Refresh(RefreshMode.StoreWins, one);
                Console.WriteLine("/r/n调用RefreshMode.StoreWins后从数据库刷新当前对象:/r/n编号:{0} 姓名:{1} 年龄:{2} 实体状态:{3}", one.id, one.name, one.age, one.EntityState.ToString());


                one.age = 90;
                Console.WriteLine("/r/n修改年龄为90后:/r/n编号:{0} 姓名:{1} 年龄:{2} 实体状态:{3}", one.id, one.name, one.age, one.EntityState.ToString());

                context.Refresh(RefreshMode.StoreWins, one);
                Console.WriteLine("/r/n调用RefreshMode.StoreWins后从数据库刷新当前对象,之前修改丢失:/r/n编号:{0} 姓名:{1} 年龄:{2} 实体状态:{3}", one.id, one.name, one.age, one.EntityState.ToString());
            }

            Console.WriteLine("按任意键退出");
            Thread thread = new Thread(command);
            thread.IsBackground = true;
            thread.Start();

            while (!exit)
            {
            }
        }

        static bool exit = false;
        static void command()
        {
            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                if (key != null) break;
            }
            exit = true;
        }
    }
}

 

测试脚本:

 

create database test;
go

use test;
go
create table person
(
id int primary key
,name nvarchar(20)
,age int
);
go
insert into person values(1,'king',30);
go
select * from person;

你可能感兴趣的:(理解实体模型的刷新模式RefreshMode.ClientWins)