(精华)2020年8月19日 数据库设计 EF执行内存状态详解

#region 基本内存状态变化 
using (TencentClassRoomContext context = new TencentClassRoomContext())
{
    SysUser user5 = context.SysUsers.Find(5);
    Company company = context.Companies.Find(1);
    user5.Name += "__AA";
    company.Name += "_BB";
    context.SaveChanges();
}

Company adCompany = new Company()
{
    Name = "测试一下公司~~"
};

//EF 在装填跟踪的时候: Clone 一个副本; 每次一次操作以后,会和副本比对,如果和副本不一样,状态就更新了!
using (TencentClassRoomContext context = new TencentClassRoomContext())
{
    Console.WriteLine(context.Entry<Company>(adCompany).State);  //Detached
    adCompany.Name += "_AA";
    Console.WriteLine(context.Entry<Company>(adCompany).State);//Detached
    Console.WriteLine("******************************************************");
    context.Companies.Add(adCompany);
    Console.WriteLine(context.Entry<Company>(adCompany).State); //Added
    Console.WriteLine("******************************************************");
    adCompany.Name += "——CC";
    Console.WriteLine("******************************************************");
    Console.WriteLine(context.Entry<Company>(adCompany).State);  //Added
    context.SaveChanges();
    Console.WriteLine("******************************************************");
    Console.WriteLine(context.Entry<Company>(adCompany).State);  //Unchanged

    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");

    adCompany.Name += "-CCCC";
    Console.WriteLine("******************************************************");
    Console.WriteLine(context.Entry<Company>(adCompany).State);  //Modified

    context.Companies.Remove(adCompany);
    Console.WriteLine("******************************************************");
    Console.WriteLine(context.Entry<Company>(adCompany).State);  //Deleted
    context.SaveChanges();
}

#endregion

#region 跟踪实体
{
    Company adCompany = new Company()
    {
        Name = "测试一下公司0001"
    };
    //EF 在装填跟踪的时候: Clone 一个副本; 每次一次操作以后,会和副本比对,如果和副本不一样,状态就更新了!
    using (TencentClassRoomContext context = new TencentClassRoomContext())
    {
        Console.WriteLine(context.Entry<Company>(adCompany).State);  //Detached
        context.Companies.Attach(adCompany);//让上下文去跟踪实体
        Console.WriteLine(context.Entry<Company>(adCompany).State);  //Detached
    }
}
#endregion

#region 无状态查询 
//因为有状态跟踪---clone 副本;  比较占内存
using (TencentClassRoomContext context = new TencentClassRoomContext())
{
    {
        //1.如果我明确知道查询出来的结果  不需要做操作;可以使用AsNoTracking  可以提供性能;
        var userlist = context.SysUsers.Where(a => a.Id > 3).ToList();
        Console.WriteLine(context.Entry<SysUser>(userlist[0]).State);
        var userlist1 = context.SysUsers.Where(a => a.Id > 3).AsNoTracking().ToList();
        Console.WriteLine(context.Entry<SysUser>(userlist1[0]).State);
    }
    {
        //Find查询优先到内存查找  建议大家多使用Find
        var userlist = context.SysUsers.Where(a => a.Id > 4).ToList(); //ToList  ToArray 可以把数据加载到内存
        Console.WriteLine("*************************************");
        SysUser user6 = context.SysUsers.FirstOrDefault(u => u.Id == 6);
        Console.WriteLine("*************************************");
        SysUser user8 = context.SysUsers.Find(8);  //优先从缓存查找 、再去查询数据库
        Console.WriteLine("*************************************");
        SysUser user4 = context.SysUsers.Find(4);  //优先从缓存查找 、再去查询数据库
    }
}
#endregion

#region 按需要更新
//1. 先查询  然后更改
{
    Company updateCompany = new Company()
    {
        Id = 1,
        Name = "高级班的测试数据111~~"
    };

    using (TencentClassRoomContext context = new TencentClassRoomContext())
    {
        //1. 先查询  然后更改
        {
            SysUser sysUser = context.SysUsers.Find(updateSysUsers.Id);
            sysUser.Name = updateSysUsers.Name;
            context.SaveChanges();
        }
        {
            //2.附加进来,指定为 EntityState.Modified   会修改所有字段
            context.Companies.Attach(updateCompany);
            context.Entry<Company>(updateCompany).State = EntityState.Modified;
            context.SaveChanges();
        }
        {
            //3.context.Entry(updateCompany).Property("Name").IsModified = true;指定修改的字段状态
            context.Companies.Attach(updateCompany);
            context.Entry(updateCompany).Property("Name").IsModified = true;
            context.SaveChanges();
        }
    }

}
#endregion

你可能感兴趣的:(数据库)