I am developing an WPF application with EF 6 database first approach, I am have 1 project in my solutions, if i run my project this error always appear.
我正在开发一个使用EF 6数据库第一种方法的WPF应用程序,我在我的解决方案中有一个项目,如果我运行我的项目,这个错误总会出现。
The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
在Code First模式中使用上下文,其中包含从EDMX文件生成的用于Database First或Model First开发的代码。这将无法正常工作。要解决此问题,请不要删除引发此异常的代码行。如果您希望使用Database First或Model First,请确保Entity Framework连接字符串包含在启动项目的app.config或web.config中。如果要创建自己的DbConnection,请确保它是EntityConnection而不是其他类型的DbConnection,并将其传递给采用DbConnection的基本DbContext构造函数之一。要了解有关Code First,Database First和Model First的更多信息,请参阅此处的Entity Framework文档:http://go.microsoft.com/fwlink/?LinkId = 394715
6 个解决方案
#1
9
My mistake was using standard connection string in constructor
我的错误是在构造函数中使用标准连接字符串
(Server = test\test; Database = DB; User Id = test_user;Password = test),
(Server = test \ test; Database = DB; User Id = test_user; Password = test),
but Entity Framework needs different format
但实体框架需要不同的格式
(metadata=res:///DBModel.csdl|res:///DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=test\test;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient)
(metadata = res:///DBModel.csdl | res:///DBModel.ssdl | res://*/DBModel.msl; provider = System.Data.SqlClient; provider connection string =“data source = test \ test ; initial catalog = DB; integrated security = True; MultipleActiveResultSets = True; App = EntityFramework“”“providerName =”“System.Data.EntityClient”
#2
6
EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you receive this error you can add the metadata section to the connection string in your config file.
EF根据连接字符串中是否存在元数据部分进行假设。如果收到此错误,可以将元数据部分添加到配置文件中的连接字符串。
E.g. if your connection string looks like this:
例如。如果您的连接字符串如下所示:
<add name="MyModel" connectionString="data source=SERVER\INSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
Prepend metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;
so that it looks like this:
Prepend metadata = res://*/MyModel.csdl | res://*/MyModel.ssdl | res://*/MyModel.msl;所以它看起来像这样:
<add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVER\INSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
#3
3
One thing you can do is... (if is Database first)
你可以做的一件事是......(如果是数据库第一)
Open the .edmx[Diagram] -> right click -> "Update Model from database"
打开.edmx [Diagram] - >右键单击 - >“从数据库更新模型”
And see if the will appear the "Add", "Refresh" and "Delete" tabs.
并查看是否会出现“添加”,“刷新”和“删除”标签。
If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)
如果没有...可能您的连接中断,并且VS的对话框将创建一个新的连接字符串。 =)
#4
1
You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string
section of app.config
:
您不应该使用生成的连接字符串,现在您的解决方案中包含所有元数据文件。而是尝试在app.config的连接字符串部分中使用:
"data source=localhost\sqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"
#5
0
I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".
我也面对这个完全相同的消息,但是使用web MVC项目。当我尝试从导入的模型自动生成Controller时,会触发此消息。它似乎无法正常工作,因为“这是从EDMX文件生成的”。
The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.
好消息是,如果我生成基于“Code First”而不是“EF Designer”的模型,它就有效。坏消息是,如果我希望自动控制器生成工作,我就无法使用EF Designer。从这两种方式生成模型无关紧要。生成模型后,您将以相同的方式使用它。
Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!
尝试从项目中删除所有emdx对象,并基于Code First而不是EF Designer重新创建模型。为我工作!
#6
0
Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api. My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:
很晚但仍然很有帮助。我遇到了类似的问题。发布了关于SO的问题,并且能够找到解决方案。您可以参考C#Web Api中的连接字符串错误。我的情况是我在web.config中有两个连接字符串(你会在去链接时知道原因)。评论一个字符串会引发您在评论时得到的错误,另一个字符串引发错误,如下所示:
An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.
尝试创建“AccountController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。
what i did was: I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string. This solved my problem.
我做的是:我将我的第一个连接字符串命名为DefaultConnection,在ApplicationDbContext类构造函数中,我给出了这个DefaultConnection。现在我的AccountController使用此连接字符串,所有其他控制器使用第二个连接字符串。这解决了我的问题。
#1
9
My mistake was using standard connection string in constructor
我的错误是在构造函数中使用标准连接字符串
(Server = test\test; Database = DB; User Id = test_user;Password = test),
(Server = test \ test; Database = DB; User Id = test_user; Password = test),
but Entity Framework needs different format
但实体框架需要不同的格式
(metadata=res:///DBModel.csdl|res:///DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=test\test;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient)
(metadata = res:///DBModel.csdl | res:///DBModel.ssdl | res://*/DBModel.msl; provider = System.Data.SqlClient; provider connection string =“data source = test \ test ; initial catalog = DB; integrated security = True; MultipleActiveResultSets = True; App = EntityFramework“”“providerName =”“System.Data.EntityClient”
#2
6
EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you receive this error you can add the metadata section to the connection string in your config file.
EF根据连接字符串中是否存在元数据部分进行假设。如果收到此错误,可以将元数据部分添加到配置文件中的连接字符串。
E.g. if your connection string looks like this:
例如。如果您的连接字符串如下所示:
<add name="MyModel" connectionString="data source=SERVER\INSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
Prepend metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;
so that it looks like this:
Prepend metadata = res://*/MyModel.csdl | res://*/MyModel.ssdl | res://*/MyModel.msl;所以它看起来像这样:
<add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVER\INSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
#3
3
One thing you can do is... (if is Database first)
你可以做的一件事是......(如果是数据库第一)
Open the .edmx[Diagram] -> right click -> "Update Model from database"
打开.edmx [Diagram] - >右键单击 - >“从数据库更新模型”
And see if the will appear the "Add", "Refresh" and "Delete" tabs.
并查看是否会出现“添加”,“刷新”和“删除”标签。
If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)
如果没有...可能您的连接中断,并且VS的对话框将创建一个新的连接字符串。 =)
#4
1
You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string
section of app.config
:
您不应该使用生成的连接字符串,现在您的解决方案中包含所有元数据文件。而是尝试在app.config的连接字符串部分中使用:
"data source=localhost\sqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"
#5
0
I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".
我也面对这个完全相同的消息,但是使用web MVC项目。当我尝试从导入的模型自动生成Controller时,会触发此消息。它似乎无法正常工作,因为“这是从EDMX文件生成的”。
The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.
好消息是,如果我生成基于“Code First”而不是“EF Designer”的模型,它就有效。坏消息是,如果我希望自动控制器生成工作,我就无法使用EF Designer。从这两种方式生成模型无关紧要。生成模型后,您将以相同的方式使用它。
Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!
尝试从项目中删除所有emdx对象,并基于Code First而不是EF Designer重新创建模型。为我工作!
#6
0
Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api. My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:
很晚但仍然很有帮助。我遇到了类似的问题。发布了关于SO的问题,并且能够找到解决方案。您可以参考C#Web Api中的连接字符串错误。我的情况是我在web.config中有两个连接字符串(你会在去链接时知道原因)。评论一个字符串会引发您在评论时得到的错误,另一个字符串引发错误,如下所示:
An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.
尝试创建“AccountController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。
what i did was: I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string. This solved my problem.
我做的是:我将我的第一个连接字符串命名为DefaultConnection,在ApplicationDbContext类构造函数中,我给出了这个DefaultConnection。现在我的AccountController使用此连接字符串,所有其他控制器使用第二个连接字符串。这解决了我的问题。