Entity Framework 4.1 Code First Freeze

拥有回忆 提交于 2019-12-10 11:34:53

问题


I'm having trouble getting EF 4.1 working on my computer. It seems to be some problem with my database settings. I was trying out this simple walkthrough: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx

But when it reaches db.Categories.Add(food); it just freeze.

I have a normal SQL Server 2008 R2 installed, not SQL Express. There also seems to be some problems with creating .mdf files instead of a direct connection to the localhost SQL server.

I've also tried adding an entity model with a database connection, but this does not seem to work. Do anyone have any pointers for me.

Thanks for any answers :)

edit: I now get a System.Data.ProviderIncompatibleException with "Vendor returned no ProviderMaifestToken-string"


回答1:


I guess this is because Database Re-Initialization. If your EF code try to drop and create database with SQL Management Studion open or connected. This situation comes up.




回答2:


I found the problem. These tutorials do not contain information about some vital adjustments that I needed to make it work.

1) Your application must have an App.config containing a connectionstring named with the same name as your class that inherited from DbContext. In my case "TestEF_CF.ProductContext".

2) The database cannot be created before you start to use it. Just set the Initial Catalogue to the name you want Entity Framework to create when it autocreate the database.

Once I did this, it worked properly. I got further with the first step, but got an exception when I tried to save to the database. When EF autocreated the database everything worked fine.

One thing that may have made it more problematic on my development environment could be the fact that I cannot use local mdf files directly. Still it would not hurt if the EF team could share these details more open than they do now, it would save me (or us) some frustration.




回答3:


Try setting the datasource on the connection string to .\SQLEXPRESS

It did work for me



来源:https://stackoverflow.com/questions/6686693/entity-framework-4-1-code-first-freeze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!