mvc3 code-first error on sqlexpress
When using sqlexpress
<add name="SchoolContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=CatomMvc开发者_开发问答;Integrated Security=True"
providerName="System.Data.SqlClient" />
I get an error
Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
When I change to
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0"/>
It's work but I want to use sqlexpress or sql 2008 that gave the same error.
The database CatomMvc
may already exist in .\sqlexpress
.
Try to rename the database to something like Catomvc_backup
or drop the database, then try again.
Alternatively you could use the following within your Application_Start()
:
Database.SetInitializer(new DropCreateDatabaseAlways<YourContext>());
This will force the Database to be recreated, but generally during development you should only use:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourContext>());
To allow the Database to be recreated based on changes to your model.
However, neither of these should make it into your production code!
Just make sure your Database.SetInitializer
function is at the top of your Application_Start
function.
Does the trick :)
Sometime it happens because you dont have sysadmin role on local SQL Server instance. I found useful article where you can download script command and install it. You will be ask for SQLEXPRESS name or instance name, just you can write 'SQLEXPRESS' and enter. Thats it. It will add you to sysadmin role of local server instance.
Download command from: http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=addselftosqlsysadmin&DownloadId=9198
Please let me know if it does not work for you.
精彩评论