MVC自動生成數據庫【Code-FIrst方式】


一般我們寫好實體之后,配置好數據上下文對象,還有在配置文件中改好連接字符串之后。
還不能生成數據庫,自動生成數據庫,有兩步關鍵步驟:
 
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        ---執行玩這個之后,在數據庫中就生成了我們配置好的數據庫,數據表
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM