MVC自動產生資料庫【Code-FIrst方式】

來源:互聯網
上載者:User

標籤:

一般我們寫好實體之後,配置好資料內容對象,還有在設定檔中改好連接字串之後。還不能產生資料庫,自動產生資料庫,有兩步關鍵步驟: 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        ---執行玩這個之後,在資料庫中就產生了我們配置好的資料庫,資料表   

MVC自動產生資料庫【Code-FIrst方式】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.