EF原理和五种状态管理

原文连接 https://www.cnblogs.com/fuyouchen/p/9388251.html

一、原理:

       如何查看真正执行的SQL是怎样的? 

             DbContext有一个Database属性,Database属性有一个Log属性,是Action委托类型其中的参数就是sql语句,每次EF执行sql语句的时候都会执行Log,因此就知道执行了什么sql;

             EF的查询是“延迟执行”的,只有遍历结果集的时候才执行select查询,Tolist()内部也是遍历结果集形成的List;

复制代码
        static void Main(string[] args)
        {
            using (TestDbContext ctx = new TestDbContext())
            {
                ctx.Database.Log = sql =>
                {
                    Console.WriteLine(sql);
                };
                var ps= ctx.Persons.Where(p => p.Id <4);
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">开始执行了</span><span style="color: #800000;">"</span><span style="color: #000000;">);


             </span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> p <span style="color: #0000ff;">in</span><span style="color: #000000;"> ps)
             {
                 Console.WriteLine(p.ToString());
             }
             
            Console.ReadKey();
        }
    }</span></pre>
复制代码
                long[] ids = { 2, 5, 8 };
                var result = ctx.Persons.Where(p => ids.Contains(p.Id));
result.toList();
                //二次过滤,在sql中自动拼接。
                long[] ids = { 2, 5, 8 };
                var result = ctx.Persons.Where(p => ids.Contains(p.Id));
                result = result.Where(p => p.Name.Length > 5);

              EF多次指定where来实现动态的复合检索; select返回值必须写成IQueryable<Person> 

              EF是夸数据库的,如果迁移到Mysql数据库上,就会编译成Mysql语法。要配置对应数据库的EntityFrameworkProvider

              每次执行_MigrationHistory 是DBMigration用的,也就是EF帮我们建数据库,可以禁用:

                Database.SetInitializer<****DbContext>(null)

二、执行原始Sql语句:

         在一些特殊的场合,需要执行原生的SQL。

         执行非查询语句:调用DbContext 的Database 属性的ExecutesqlCommand 方法,可以通过占位符的方式传递参数:

复制代码
            using (TestDbContext ctx = new TestDbContext())
            {
            </span><span style="color: #0000ff;">string</span> name = <span style="color: #800000;">"</span><span style="color: #800000;">Tony</span><span style="color: #800000;">"</span><span style="color: #000000;">;
            ctx.Database.ExecuteSqlCommand(</span><span style="color: #800000;">"</span><span style="color: #800000;">insert into T_Persons(Name,CreateDateTime) values({0},GetDate())</span><span style="color: #800000;">"</span><span style="color: #000000;">, name);

            Console.ReadKey();
        }</span></pre>
复制代码

        执行查询语句: 

复制代码
        static void Main(string[] args)
        {
            using (TestDbContext ctx = new TestDbContext())
            {
           </span><span style="color: #0000ff;">var</span> result=  ctx.Database.SqlQuery&lt;GroupCount&gt;(<span style="color: #800000;">"</span><span style="color: #800000;"> select Age,COUNT(*) as GroupCounts from T_Persons group by Age </span><span style="color: #800000;">"</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">in</span><span style="color: #000000;"> result)
            {
                Console.WriteLine(item.Age</span>+<span style="color: #800000;">"</span><span style="color: #800000;">=</span><span style="color: #800000;">"</span>+<span style="color: #000000;">item.GroupCounts);
            }

            Console.ReadKey();
        }
    }</span></pre>
复制代码
   public class GroupCount
    {
        public int Age { get; set; }
        public int GroupCounts { get; set; }
    }

三、EF对象的状态

        EF中的对象有五个状态:  Derached(游离态,脱离态),Unchange(未改变),Added(新增),Deleted(删除),Modified(被修改)

 

new 出来的对象为Derached状态,可以通过Add() 方法成为Added状态,然后SaveChange()保存到数据,查询出该数据为Unchange状态。
原文地址:https://www.cnblogs.com/sunny3158/p/11767029.html