Why do I get this error when I try to open an MDF database via path/filename in Entity Framework?

南楼画角 提交于 2019-12-13 02:54:25

问题


I'm migrating an application from LINQ-to-SQL to Entity Framework and have changed the line:

using (var db = new MainDataContext(SystemHelpers.GetDatabaseConnectionString()))

to

using (var db = new MainDataEntities(SystemHelpers.GetDatabaseConnectionString()))

where SystemHelpers.GetDatabaseConnectionString()) is a file path to an .mdf file.

It works in LINQ-to-SQL but in Entity Framework the above line gives me this error:

The format of the initialization string conflicts with the specification which begins with '0';

which is the best translation I can do from the German:

"Das Format der Initialisierungszeichenfolge stimmt nicht mit der Spezifikation überein, die bei Index '0' beginnt."


回答1:


Entity Framework connections string are more complicated than standard connection strings.

They are made up of three parts:

  1. the Provider Connection String => this is what you're providing
  2. the Metadata => which is where the EF should get the CSDL, MSL and SSDL from (i.e. Conceptual Model, Mapping and Storage Model)
  3. the Provider name => i.e. for SQL server this is generally System.Data.SqlClient

Here is what it one might look like:

metadata=res:///Model.csdl|res:///Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True"

Hope this helps

Alex




回答2:


EF connection strings can't be the DB connection string alone. They contain the DB connection string, but they have much more information, too. It sounds like you're trying to use the DB connection string alone. That won't work. See this example for how to build an EF connection string.



来源:https://stackoverflow.com/questions/1708964/why-do-i-get-this-error-when-i-try-to-open-an-mdf-database-via-path-filename-in

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