实体框架代码第一和连接字符串 [英] Entity Framework Code First and Connection Strings

查看:73
本文介绍了实体框架代码第一和连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用实体框架代码的小型MVC 3应用程序,并使用此连接字符串作为模型:

 数据源= .\SQLEXPRESS;集成安全性= SSPI; AttachDBFilename = | DataDirectory | Journal.mdf;用户实例= true;数据库= MyJournal 

当我更改模型(例如添加一个属性)时,我会按预期的方式获得


模型自从创建数据库后,JournalContext上下文已经更改。


所以,在开发模式下,我继续删除日志.mdf和Journal.ldf。



现在,当我再次运行应用程序时,我得到


无法打开登录请求的数据库MyJournal。

如果我将连接字符串更改为

  data source = .\SQLEXPRESS; Integrated Security = SSPI; AttachDBFilename = | DataDirectory | Journal.mdf; User Instance = true; Database = MyJournal2 
Database =
参数

p $ p

创建Journal.mdf并再次运行应用程序。如果我做了一些修改,并尝试再次回收任何数据库名称,我会收到无法打开错误。



为什么我需要提供一个唯一的数据库名称每次更改模型时,如何清除以前的名字?

解决方案

您不需要每次唯一的数据库名称。当模型首次创建时,它会运行一个DatabaseInitializer来执行诸如创建数据库(如果不存在)或添加种子数据的操作。默认的DatabaseInitializer尝试将使用模型所需的数据库模式与存储在使用数据库创建的EdmMetadata表中的模式的散列进行比较(当Code First是创建数据库时)。如果哈希比较是不同的,那么它会抛出该错误。



显然,如果更改连接字符串,那么它将创建一个名为MyJournal2的全新数据库。 p>

解决此问题的方法是删除EdmMetadata表并再次运行初始化程序。您可以通过进入Visual Studio中的数据库浏览器窗口并连接到数据库,然后转到表,您应该找到EdmMetadata表,右键单击它并选择删除。



或者放置

  DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges< dbType>()); Global.asax中的 Application_Start 方法中的

的.cs。这将删除数据库,并在架构更改时重新创建它。



请参阅类更改部分。



还要检查此链接 DropCreateDatabaseIfModelChanges 。它告诉您实际发生的情况以及如何通过创建派生类来种子数据库。


I have a small MVC 3 app using Entity Framework Code First and use this connection string for the model:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Journal.mdf;User Instance=true;Database=MyJournal

When I make a change to the model (e.g. add a property), I get as expected

The model backing the 'JournalContext' context has changed since the database was created.

So, being in development mode, I go ahead and delete Journal.mdf and Journal.ldf.

Now when I run the application again, I get

Cannot open database "MyJournal" requested by the login. The login failed.

If I change my connection string to

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Journal.mdf;User Instance=true;Database=MyJournal2

(changed the Database= parameter by appending '2')

Journal.mdf is created and the app works again. If I make a number of changes and attempt to "recycle" any Database name again, I get the "Cannot open" error.

Why do I need to provide a unique Database name each time I change the model, and how can I "clean out" previous names?

解决方案

You don't need a unique database name each time. When a model is first created, it runs a DatabaseInitializer to do things like create the database if it's not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). If the hash comparison is different then it throws that error.

Obviously if you change the connection string then its going to create an entirely new database called 'MyJournal2'.

Ways to get around this are to delete the EdmMetadata table and run the initializer again. You can do this by going into the Database Explorer window in Visual Studio and connecting to your database, then going to Tables where you should find the EdmMetadata table, right-clicking on it and selecting Delete.

Alternatively put

DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<dbType>());

in your Application_Start method in Global.asax.cs. This will delete the database and recreate it whenever the schema changes.

See this video on pluralsight for more details, especially the section 'When classes change'.

Also check this link for DropCreateDatabaseIfModelChanges. It tells you about what actually happens and how to seed the database if you need to by creating a derived class.

这篇关于实体框架代码第一和连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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