zoukankan      html  css  js  c++  java
  • EF三种编程方式详细图文教程(C#+EF)之Code First

    Code First

    Code First模式我们称之为“代码优先”模式,是从EF4.1开始新建加入的功能。使用Code First模式进行EF开发时开发人员只需要编写对应的数据类(其实就是领域模型的实现过程),然后自动生成数据库。这样设计的好处在于我们可以针对概念模型进行所有数据操作而不必关系数据的存储关系,使我们可以更加自然的采用面向对象的方式进行面向数据的应用程序开发。

    从某种角度来看,其实“Code First”和“Model First”区别并不是太明显,只是它不借助于实体数据模型设计器,而是直接通过编码(数据类)方式设计实体模型(这也是为什么最开始“Code First”被叫做“Code Only”的原因)。但是对于EF它的处理过程有所差别,例如我们使用Code First就不再需要EDM文件,所有的映射通过“数据注释”和“fluent API”进行映射和配置。另外需要注意的是“Code First”并不代表一定就必须通过数据类来定义模型,事实上也可以通过现有数据库生成数据类。

    那么我们首先看一下传统的Code First如何使用。

    首先创建一个控制台应用程序,接下来添加两个类“Order”和“OrderDetail”,我们可以看到这两个类只是简单的C#对象(POCO,Plain Old C# Object)这两个类基本和EF没有任何关系,需要注意的是这两个类有两个导航属性“Order.OrderDetails”和“OrderDetail.Order”:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoEF
    {
        class Order
        {
            public int Id { get; set; }
            public string Customer { get; set; }
            public System.DateTime OrderDate { get; set; }
    
            public virtual List<OrderDetail> OrderDetails { get; set; }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoEF
    {
        class OrderDetail
        {
            public int Id { get; set; }
            public string Product { get; set; }
            public string UnitPrice { get; set; }
            public int OrderId { get; set; }
    
            public virtual Order Order { get; set; }
        }
    }

    有了这两个类之后让我们定义一个数据库上下文,有了它我们就可以对数据进行增删改查操作了,这个类必须继承于"System.Data.Entity.DbContext”类以赋予它数据操作能力。因此接下来我们需要给这个应用安装EntityFramework包,因为到目前为止我们并没有引入EF框架相关的任何内容,我们需要引入EF相关程序集。但是我们有更好的选择那就是NuGet。通过NuGet进行在线安装:项目中右键选择"Manage NuGet Packages…”;选择Online;再选择“EntityFramework”;然后点击安装即可。

    数据库上下文操作类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoEF
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var ctx = new OrderContext())
                {
    
                    var o = new Order();
                    o.OrderDate = DateTime.Now;
                    ctx.Orders.Add(o);
                    ctx.SaveChanges();
    
                    var query = from order in ctx.Orders
                                select order;
                    foreach (var q in query)
                    {
                        Console.WriteLine("OrderId:{0},OrderDate:{1}", q.Id, q.OrderDate);
                    }
    
                    Console.Read();
                }
            }
        }
    }

    运行结果:

    如果是第一次使用EF Code First的朋友一定会有疑问,我们没有进行任何数据库配置,增加了一条数据通过查询确实保存上了,那么我们的数据到底在哪呢?事实上如果用户不进行数据库配置EF默认会使用“.SQLEXPRESS”数据库实例,如果你没有安装“.SQLEXPRESS”则默认使用LocalDb,关于LocalDb的具体细节请看:SQL Server 2012 Express LocalDB。我们可以在这里找到系统自动创建的数据库:

    但是多数情况下我们是希望自己控制这个数据库的,例如我想让他保存在我机器上的”.SQL2008”实例上,此时我们就需要在配置文件App.Config中配置一个数据库连接串,然后在我们的数据库上下文中指定这个连接名称。

    App.config配置文件:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
       <connectionStrings>
        <add name="CodeFirstDb" connectionString="Data Source=LENOVOSQLEXPRESS;Database=CodeFirstDb;UID=sa;PWD=sql;" providerName="System.Data.SqlClient"></add>
      </connectionStrings> 
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>

     

    OrderContext类,构造函数多了一个连接名参数:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    
    namespace DemoEF
    {
        class OrderContext:DbContext
        {
            public OrderContext(string connectionName)
                : base(connectionName)
            {
            }
            public DbSet<Order> Orders
            {
                get;
                set;
            }
    
            public DbSet<OrderDetail> OrderDetails
            {
                get;
                set;
            }
        }
    }

    使用的时候,传入配置的数据库连接字符串名称:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoEF
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var ctx = new OrderContext("CodeFirstDb"))
                {
    
                    var o = new Order();
                    o.OrderDate = DateTime.Now;
                    ctx.Orders.Add(o);
                    ctx.SaveChanges();
    
                    var query = from order in ctx.Orders
                                select order;
                    foreach (var q in query)
                    {
                        Console.WriteLine("OrderId:{0},OrderDate:{1}", q.Id, q.OrderDate);
                    }
    
                    Console.Read();
                }
            }
        }
    }

    执行之后就会发现在”.sqlexpress”实例上多了一个“CodeFirstDb”数据库(注意图中除了我们创建的两个实体表还有一个系统表dbo._MigrationHistory它记录了模型的定义,在以后的文章中我会着重解释此表):

    到了这里我们三种EF编程方式已经全部介绍完了。

     

    本文大部分仿照崔江涛(KenshinCui),写的博客

    Entity Framework 5.0系列之EF概览

     

  • 相关阅读:
    P1144 最短路计数 题解 最短路应用题
    C++高精度加减乘除模板
    HDU3746 Teacher YYF 题解 KMP算法
    POJ3080 Blue Jeans 题解 KMP算法
    POJ2185 Milking Grid 题解 KMP算法
    POJ2752 Seek the Name, Seek the Fame 题解 KMP算法
    POJ2406 Power Strings 题解 KMP算法
    HDU2087 剪花布条 题解 KMP算法
    eclipse创建maven项目(详细)
    maven的作用及优势
  • 原文地址:https://www.cnblogs.com/zrdm/p/5060412.html
Copyright © 2011-2022 走看看