实体框架6.1代码优先不创建数据库 [英] Entity Framework 6.1 code-first not creating database
本文介绍了实体框架6.1代码优先不创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
iam使用此代码,但不要使用表格创建数据库
i dont问题在哪里
iam use this code but dont create database with tables
i dont where is problem
<pre lang="xml"><connectionStrings>
<add name="SchoolDBContext" connectionString="data source=home;initial catalog=School;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings>
</pre>
<pre lang="c#">
<pre lang="xml">namespace unviresty.Dal
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using unviresty.Models;
public partial class SchoolContext : DbContext
{
public SchoolContext()
: base("name=SchoolDBContext")
{
Database.SetInitializer<SchoolContext>(new CreateDatabaseIfNotExists<SchoolContext>());
}
public DbSet<student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
}
</pre>
<pre lang="c#">
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
</pre>
推荐答案
<pre lang="c#">
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseIfModelChanges<schoolcontext>());
using (var context = new SchoolContext())
{
context.Database.Initialize(force: true);
}
}
这篇关于实体框架6.1代码优先不创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文