MVC自动生成数据库【Code-FIrst方式】
2016-04-29 11:39
357 查看
一般我们写好实体之后,配置好数据上下文对象,还有在配置文件中改好连接字符串之后。
还不能生成数据库,自动生成数据库,有两步关键步骤:
1. Enable Migrations
2. Update-database
执行玩第二步之后,就自动生成了数据库。
------------------------------------实例----------------------------------------------------------------
//实体:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace PagingAndSortingInMvc.Entities
{
public class EmployeeMaster
{
[Key]
public string ID { get; set; }
[Required(ErrorMessage="请输入员工名字")]
public string Name { get; set; }
[Required(ErrorMessage="请输入手机号码")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage="请输入Email")]
public string Email { get; set; }
[Required(ErrorMessage= "请输入薪水")]
public decimal Salary { get; set; }
}
}
-------------------------------------------------------------------------------------------------------------
创建的数据上下文类:
using PagingAndSortingInMvc.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace PagingAndSortingInMvc.Models
{
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext()
: base("name=ConnectionString")
{ }
public DbSet<EmployeeMaster> Employees { get; set; }
}
}
-----------------------------------------------------------------------------------------------------
配置文件中的数据库连接字符串:
<connectionStrings>
<add name="ConnectionString" connectionString="server=.;database=PageAndSortDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>
----------------------------------------------------------------------------------------------------------------------------------------------------------
设置这些之后,编译一下项目:
就运行:
1. Enable Migrations----------会生成一个类文件【Configuration.cs】,改一下AutomaticMigrationsEnabled 属性为true
namespace PagingAndSortingInMvc.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<PagingAndSortingInMvc.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(PagingAndSortingInMvc.Models.ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}
2. Update-database ---执行玩这个之后,在数据库中就生成了我们配置好的数据库,数据表
还不能生成数据库,自动生成数据库,有两步关键步骤:
1. Enable Migrations
2. Update-database
执行玩第二步之后,就自动生成了数据库。
------------------------------------实例----------------------------------------------------------------
//实体:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace PagingAndSortingInMvc.Entities
{
public class EmployeeMaster
{
[Key]
public string ID { get; set; }
[Required(ErrorMessage="请输入员工名字")]
public string Name { get; set; }
[Required(ErrorMessage="请输入手机号码")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage="请输入Email")]
public string Email { get; set; }
[Required(ErrorMessage= "请输入薪水")]
public decimal Salary { get; set; }
}
}
-------------------------------------------------------------------------------------------------------------
创建的数据上下文类:
using PagingAndSortingInMvc.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace PagingAndSortingInMvc.Models
{
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext()
: base("name=ConnectionString")
{ }
public DbSet<EmployeeMaster> Employees { get; set; }
}
}
-----------------------------------------------------------------------------------------------------
配置文件中的数据库连接字符串:
<connectionStrings>
<add name="ConnectionString" connectionString="server=.;database=PageAndSortDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>
----------------------------------------------------------------------------------------------------------------------------------------------------------
设置这些之后,编译一下项目:
就运行:
1. Enable Migrations----------会生成一个类文件【Configuration.cs】,改一下AutomaticMigrationsEnabled 属性为true
namespace PagingAndSortingInMvc.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<PagingAndSortingInMvc.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(PagingAndSortingInMvc.Models.ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}
2. Update-database ---执行玩这个之后,在数据库中就生成了我们配置好的数据库,数据表
相关文章推荐
- MySQL5.7重置 root 密码
- SQLServer2008新建链接服务器for Oracle
- MySQL创建用户与授权方法
- mysql一主多从同步配置
- Mongodb 创建索引
- Redis --> 为redis分配新的端口
- nagios监控mongodb
- MySQL:索引工作原理及使用
- Redis常用命令速查 <第二篇>
- sql详解
- Redis介绍及常用命令
- 第7讲-SQL语言复杂查询--(NOT)Exists
- SQL Server 批量插入数据的两种方法
- redis 基本命令学习一(strings、hashes、lists)
- SQL架构信息读取
- 批量修改数据库中字段的数据类型
- mysql常用操作
- 大并发大数量中的MYSQL瓶颈与NOSQL介绍
- oracle存储过程模板
- mysql-5.7.12-winx64安装版配置、使用