• 大小: 1.82M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-26
  • 语言: C#
  • 标签: EF  实例  c  

资源简介

一、EF Code First简介

EntityFramework 代码优先

 

二、EF Code First第一个简单实例

1、开发环境及数据库说明

开发环境:Visual Studio 2010 Ultimate sp1 Sql Server 2008 R2

数据库:Northwind

2、实例代码结构

结构说明:

App:控制台应用程序

Data:数据访问

Domain:实体类

3、安装Entity Framework

  在Visual Studio编辑器中点击Tools -> Library Package Manager -> Package Manager Console,在Package Manager Console窗口中执行下面语句,安装最新版Entity Framework。

PM> Install-Package EntityFramework

  App层和Data层分别添加对EntityFramework的引用:

 

  在App层安装EntityFramework之后,将自动添加App.config和packages.config文件。

  App.config配置Entity Framework版本信息及数据库连接信息,修改其中数据连接信息以适应本地实际环境。

 1 <?xml version="1.0" encoding="utf-8"?>  2 <configuration>  3 <configSections>  4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  5 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />  6 </configSections>  7 <entityFramework>  8 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">  9 <parameters> 10 <!--<parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />--> 11 <parameter value="Data Source=(local); User ID=sa; Password=; MultipleActiveResultSets=True" /> 12 </parameters> 13 </defaultConnectionFactory> 14 </entityFramework> 15 </configuration>

  packages.config现实当前项目使用的package:

1 <?xml version="1.0" encoding="utf-8"?> 2 <packages> 3 <package id="EntityFramework" version="4.3.1" /> 4 </packages>

4、实例代码

Domain中Category.cs

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5  6 namespace Northwind.Domain.Entities  7 {  8 public class Category  9 { 10 /// <summary> 11      /// 分类ID 12      /// </summary> 13 public int CategoryID { get; set; } 14 15 /// <summary> 16      /// 分类名称 17      /// </summary> 18 public string CategoryName { get; set; } 19 } 20 }

Data中NorthwindContext.cs

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5  6 using System.Data.Entity;  7  8 using Northwind.Domain.Entities;  9 10 namespace Northwind.Data 11 { 12 public class NorthwindContext : DbContext 13 { 14 public DbSet<Category> Categories { get; set; } 15 } 16 }

App中Program.cs

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5  6 using Northwind.Data;  7 using Northwind.Domain.Entities;  8  9 namespace Northwind.App 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 Category c = new Category() { CategoryName = "电子数码" }; 16 17 using (NorthwindContext db = new NorthwindContext()) 18 { 19 db.Categories.Add(c); 20 db.SaveChanges(); 21 } 22 23 Console.WriteLine("Finish"); 24 Console.ReadKey(); 25 } 26 } 27 }

5、运行说明

  由于在上面的数据库连接字符串中并未包含指定的数据库名称,运行成功之后,将在本地数据引擎中创建如下数据库和表:

  数据库名称:Northwind.Data.NorthwindContext

  表名称:Categories

6、示例代码附件

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Northwind.Data;
using Northwind.Domain.Entities;

namespace Northwind.App
{
    class Program
    {
        static void Main(string[] args)
        {
            Category c = new Category() { CategoryName = “电子数码“ };

            using (NorthwindContext db = new NorthwindContext())
            {
                db.Categories.Add(c);
                db.SaveChanges();
            }

            Console.WriteLine(“Finish“);
            Console.ReadKey();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        889  2012-03-25 19:26  Northwind\App\App.config

     文件       3339  2012-03-25 19:12  Northwind\App\App.csproj

     文件        490  2010-03-17 22:39  Northwind\App\bin\Debug\App.vshost.exe.manifest

     文件    1060640  2012-03-25 11:51  Northwind\App\bin\Debug\Entityframework.dll

     文件    1107427  2012-03-25 11:51  Northwind\App\bin\Debug\Entityframework.xml

     文件       5120  2012-03-25 19:33  Northwind\App\bin\Debug\Northwind.App.exe

     文件        889  2012-03-25 19:26  Northwind\App\bin\Debug\Northwind.App.exe.config

     文件      13824  2012-03-25 19:33  Northwind\App\bin\Debug\Northwind.App.pdb

     文件      11600  2012-03-25 19:34  Northwind\App\bin\Debug\Northwind.App.vshost.exe

     文件        889  2012-03-25 19:26  Northwind\App\bin\Debug\Northwind.App.vshost.exe.config

     文件       4608  2012-03-25 19:12  Northwind\App\bin\Debug\Northwind.Data.dll

     文件       7680  2012-03-25 19:12  Northwind\App\bin\Debug\Northwind.Data.pdb

     文件       4608  2012-03-25 19:12  Northwind\App\bin\Debug\Northwind.Domain.dll

     文件       7680  2012-03-25 19:12  Northwind\App\bin\Debug\Northwind.Domain.pdb

     文件        764  2012-03-25 19:34  Northwind\App\obj\x86\Debug\App.csproj.FileListAbsolute.txt

     文件       6154  2012-03-25 19:33  Northwind\App\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       5120  2012-03-25 19:33  Northwind\App\obj\x86\Debug\Northwind.App.exe

     文件      13824  2012-03-25 19:33  Northwind\App\obj\x86\Debug\Northwind.App.pdb

     文件      12602  2012-03-25 19:12  Northwind\App\obj\x86\Debug\ResolveAssemblyReference.cache

     文件        118  2012-03-25 11:51  Northwind\App\packages.config

     文件        603  2012-03-25 23:50  Northwind\App\Program.cs

     文件       1338  2012-03-25 11:38  Northwind\App\Properties\AssemblyInfo.cs

     文件    1060640  2012-03-25 11:51  Northwind\Data\bin\Debug\Entityframework.dll

     文件    1107427  2012-03-25 11:51  Northwind\Data\bin\Debug\Entityframework.xml

     文件       4608  2012-03-25 19:12  Northwind\Data\bin\Debug\Northwind.Data.dll

     文件       7680  2012-03-25 19:12  Northwind\Data\bin\Debug\Northwind.Data.pdb

     文件       4608  2012-03-25 19:12  Northwind\Data\bin\Debug\Northwind.Domain.dll

     文件       7680  2012-03-25 19:12  Northwind\Data\bin\Debug\Northwind.Domain.pdb

     文件       2828  2012-03-25 19:12  Northwind\Data\Data.csproj

     文件        308  2012-03-25 23:48  Northwind\Data\NorthwindContext.cs

............此处省略66个文件信息

评论

共有 条评论