zoukankan      html  css  js  c++  java
  • 代码优先-Code First

    非常有用的两篇文章

    MSDN:Code First 迁移

    博客园:CodeFirst数据迁移(不丢失数据库原有数据)

    EF有三种开发模式:Model First,Database First 和 Code First。 本文主要介绍Code First模式,Code First即代码优先,就是先编写业务实体模型类,然后通过程序包管理器控制台创建数据库。

    一、安装EntityFramework

      在项目上右键菜单,点击“管理NuGet程序包”,搜索“EntityFramework”,在项目中安装最新版本的EntityFramework。

      

      

    二、修改配置文件

    EntityFramework安装完成后,会自动生成一个App.confing文件,内容如下:

    <?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
          </parameters>
        </defaultConnectionFactory>
      </entityFramework>
    </configuration>
    View Code

    如果不添加数据库连接字符串,数据库默认会声称在App_Data文件夹下,在一般项目中我们还需修改此配置文件,内容如下:

    <?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />-->
      </configSections>
      <!--<entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
            <parameter value="shz-pcsqlexpress2012"/>
          </parameters>
        </defaultConnectionFactory>
      </entityFramework>-->
      <connectionStrings>
        <add name="SAUnitOfWork" connectionString="Data Source=shz-pcSQLEXPRESS2012;Initial Catalog=AccountingSys;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
      </connectionStrings>
    </configuration>
    View Code

    三、创建实体模型类和DbContext

    贴出部分实例代码:

    /// <summary>
        /// 用户信息表
        /// </summary>
        public class User : Entity
        {
            [Required]
            [StringLength(50)]
            /// <summary>
            /// 用户名
            /// </summary>
            public string Name { get; set; }
    
            [Required]
            [StringLength(20)]
            /// <summary>
            /// 登录Id
            /// </summary>
            public string LoginId { get; set; }
    
            [Required]
            [StringLength(20)]
            /// <summary>
            /// 登录密码
            /// </summary>
            public string Password { get; set; }
    
            [StringLength(11)]
            /// <summary>
            /// 手机号
            /// </summary>
            public string Mobile { get; set; }
    
            [StringLength(100)]
            /// <summary>
            /// 邮箱
            /// </summary>
            public string Email { get; set; }
    
            [Required]
            /// <summary>
            /// 用户状态(0:禁用;1:启用)
            /// </summary>
            public int Status { get; set; }
    
        }
    View Code

    以下是数据上下文类:

    public class SAUnitOfWork : DbContext, IQueryableUnitOfWork
        {
            public DbSet<Operation> Operation { get; set; }
            public DbSet<PurchaseDetail> PurchaseDetail { get; set; }
            public DbSet<PurchaseHeader> PurchaseHeader { get; set; }
            public DbSet<Role> Role { get; set; }
            public DbSet<RoleOperations> RoleOperations { get; set; }
            public DbSet<SettlementDetail> SettlementDetail { get; set; }
            public DbSet<SettlementHeader> SettlementHeader { get; set; }
            public DbSet<User> User { get; set; }
            public DbSet<UserRoles> UserRoles { get; set; }
    }
    View Code

    四、启用程序包管理器控制台

    在VS菜单栏“工具”→“库程序包管理器”→打开“程序包管理器控制台”:

    1.启用 Code First 迁移命令:Enable-Migrations
    2.为迁移搭建基架:Add-Migration Initial
    3.开始迁移:Update-Database

    执行完以上三个命令后,你会发现在你的数据库服务实例中已经生成了数据库:

  • 相关阅读:
    [POJ]poj2632(模拟)
    [EOJ]2019 ECNU XCPC March Selection #2
    [POJ]POJ1328(trie)
    卡特兰数相关总结
    2019海亮夏令营随笔
    树上数数 题解
    护卫小队 题解
    洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题解
    洛谷 P4735 最大异或和
    登峰造极 题解
  • 原文地址:https://www.cnblogs.com/shaomenghao/p/3348922.html
Copyright © 2011-2022 走看看