简体   繁体   English

当我尝试通过Entity Framework中的路径/文件名打开MDF数据库时,为什么会出现此错误?

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

I'm migrating an application from LINQ-to-SQL to Entity Framework and have changed the line: 我正在将应用程序从LINQ-to-SQL迁移到Entity Framework ,并更改了这一行:

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 . 其中SystemHelpers.GetDatabaseConnectionString()).mdf文件文件路径

It works in LINQ-to-SQL but in Entity Framework the above line gives me this error : 它在LINQ-to-SQL中有效,但在Entity Framework中,上一行给我这个错误

The format of the initialization string conflicts with the specification which begins with '0'; 初始化字符串的格式与以“ 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." “从最初的索引“ 0”开始,就可以在Spezifikationüberein中进行初始化。”

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 (ie Conceptual Model, Mapping and Storage Model) 元数据=>,这是EF应该从中获取CSDL,MSL和SSDL的位置(即概念模型,映射和存储模型)
  3. the Provider name => ie for SQL server this is generally System.Data.SqlClient 提供程序名称=>即对于SQL Server,通常为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" 元数据= res:// /Model.csdl|res:// /Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider连接字符串=“ Data Source =。\\ SQLEXPRESS;初始目录=数据库;集成安全性=真;池=假; MultipleActiveResultSets =真”

Hope this helps 希望这可以帮助

Alex 亚历克斯

EF connection strings can't be the DB connection string alone. EF连接字符串不能仅是数据库连接字符串。 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 . 有关如何构建EF连接字符串的信息,请参见此示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试访问数据库文件(.mdf)时为什么会出现网络错误? - Why do i get network error when i tried to access a database file(.mdf)? 当只知道文件名时,如何获取文件名的路径? - How do I get the path of a filename when only filename is known? 为什么在尝试构建时出现错误? - Why do I get an error when I try to build? 当我尝试获取值时实体框架挂起 - Entity Framework hangs when I try to get values 当我尝试映射实体(实体框架)的属性时,出现错误,类型“ __”必须是非空值类型 - When I try to map the properties for an entity (Entity Framework), I get the error the type '__' must be a non-nullable value type 当我尝试打开串口时,为什么会出现“CreateFile Failed:161”? - Why do I get “CreateFile Failed: 161” when I try to open the serial port? 实体框架6 - 更新edmx时实体类被删除,为什么?我该怎么做才能阻止它呢? - Entity Framework 6 - Entity classes get deleted when updating edmx, why is that? and what can I do to stop it? 我尝试这样做时出现模板错误? - Get a template error when I try to do this? 备份数据库MDF和实体框架 - Backup a database mdf & Entity Framework 当我在POCO实体上执行toString时,出现DataReader打开错误 - When I do a toString on a POCO Entity I get a DataReader open error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM