6.0的EntityFramework代码CreateDatabaseIfNotExists首先创建数据库 [英] EntityFramework 6.0 CreateDatabaseIfNotExists Code first to create database

查看:2900
本文介绍了6.0的EntityFramework代码CreateDatabaseIfNotExists首先创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是什么做错了。我有一个用户的DbContext的设置,当我最初创建代码优先使用PowerShell这一切工作正常工作。

What am I doing wrong. I have got a user DbContext setup and working when I originally created the Code-First with powershell it all worked fine.

我实现数据库初始化程序在应用程序启动预期。

I implemented Database Initializer as expected on application start.

Database.SetInitializer<UserDbContext>(new CreateDatabaseIfNotExists<UserDbContext>());



只是为了测试它是否真的创造我却下降了数据库的数据库,现在我卡住了数据库将不会创建。我使用SQL Server 2012中,任何想法可能是错误的。

Just to test out if it really creates the database I actually dropped the database and now I am stuck the database will not be created. I am using SQL Server 2012, any idea what could be wrong.

我得到该错误消息

System.InvalidOperationException: Migrations is enabled for context 'UserDbContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.



我试图从包管理器控制台相同,它仍然是给我同样的消息。

I have tried the same from Package Manager console and it is still give me the same message.

推荐答案

终于想通的解决方案,不知道为什么,或者是什么。改变了我的数据库初始化到MigrateDatabaseToLatestVersion而不是CreateDatabaseIfNotExists工作。

Finally figured the solutions, not sure why or what. Changed my Database initializer to MigrateDatabaseToLatestVersion instead of CreateDatabaseIfNotExists worked.

Database.SetInitializer<UserDbContext>(new MigrateDatabaseToLatestVersion<UserDbContext, Configuration>());

这篇关于6.0的EntityFramework代码CreateDatabaseIfNotExists首先创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆