MVC Code First 自动生成数据库

1.新建一个MVC项目

点击确定之后如下图,在Model文件夹下新建如下几个类文件:

Course.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace CodeFirstDemo.Models
{
    public class Course
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
    }
}

Score.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace CodeFirstDemo.Models
{
    public class Score
    {
        [Key]
        public int Id { get; set; }

        public Student Student { get; set; }

        public Course Course { get; set; }
    }
}

Student.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace CodeFirstDemo.Models
{
    public class Student
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
    }
}

建立上下文类:

StudentInfoEntities.cs:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace CodeFirstDemo.Models
{
    public class StudentInfoEntities : DbContext
    {
        public StudentInfoEntities()
            : base("name=StudentInfoEntities")
        {

        }

        public DbSet Courses { get; set; }
        public DbSet Scores { get; set; }
        public DbSet Students { get; set; }
    }
}

在web.config中配置如下:

"StudentInfoEntities" connectionString="Data Source=.; User=sa;Password=sa;Initial Catalog=StudentInfo;Integrated Security=True" providerName="System.Data.SqlClient" />

web.config全代码:

"1.0" encoding="utf-8"?>


  
    
    
"entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> "StudentInfoEntities" connectionString="Data Source=.; User=sa;Password=sa;Initial Catalog=StudentInfo;Integrated Security=True" providerName="System.Data.SqlClient" /> "webpages:Version" value="2.0.0.0" /> "webpages:Enabled" value="false" /> "PreserveLoginUrl" value="true" /> "ClientValidationEnabled" value="true" /> "UnobtrusiveJavaScriptEnabled" value="true" /> "true" targetFramework="4.0" /> "Forms"> "~/Account/Login" timeout="2880" /> namespace="System.Web.Helpers" /> namespace="System.Web.Mvc" /> namespace="System.Web.Mvc.Ajax" /> namespace="System.Web.Mvc.Html" /> namespace="System.Web.Optimization" /> namespace="System.Web.Routing" /> namespace="System.Web.WebPages" /> "false" /> "true" /> "ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> "ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> "ExtensionlessUrlHandler-Integrated-4.0" /> "ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> "ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> "ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> "urn:schemas-microsoft-com:asm.v1"> "DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" /> "1.0.0.0-4.0.0.0" newVersion="4.1.0.0" /> "DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" /> "1.0.0.0-4.0.0.0" newVersion="4.1.0.0" /> "System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> "1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> "System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> "1.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> "System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> "1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> "WebGrease" publicKeyToken="31bf3856ad364e35" /> "1.0.0.0-1.3.0.0" newVersion="1.3.0.0" /> "System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> "v11.0" />

然后在HomeController.cs中写代码如下:

using CodeFirstDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CodeFirstDemo.Controllers
{
    public class HomeController : Controller
    {
        private StudentInfoEntities db = new StudentInfoEntities();
        public ActionResult Index()
        {
            return View(db.Students.ToList());//先要执行下才能自动生成数据库
        }

    }
}

 

到此完毕,生成不了请检查web.config是否正确吧,鄙人亲自测试了的是可以自动生成数据库的

转载于:https://www.cnblogs.com/LoveQin/p/5713182.html

你可能感兴趣的:(MVC Code First 自动生成数据库)