I am using local SQL Server CE database file (.sdf
) and Entity Framework for my task.
我正在使用本地SQL Server CE数据库文件(.sdf)和实体框架来完成我的任务。
I have created my models and now entity framework should map them to local database.
我创建了我的模型,现在实体框架应该将它们映射到本地数据库。
I have this code in my Main
method which inserts some data to database(to check):
我在我的Main方法中有这个代码,它将一些数据插入数据库(检查):
var db = new Context()
var blog = new Blog { Name = 'FirstBlog'};
db.Blogs.Add(blog);
db.SaveChanges();
and this is my connection string:
这是我的连接字符串:
<connectionStrings>
<add name="Connection"
connectionString="Data source=|DataDirectory|\LocalDB.sdf""
providerName="System.Data.EntityClient" />
</connectionStrings>
when I run this, I get this error:
当我运行它时,我收到此错误:
Format of the initialization string does not conform to specification starting at index 0.
初始化字符串的格式不符合从索引0开始的规范。
I have tried several connection string but none worked.
我已经尝试了几个连接字符串但没有工作。
How can I fix this?
我怎样才能解决这个问题?
UPDATE:
As suggested I changed my connection string to be:
建议我将连接字符串更改为:
<connectionStrings>
<add name="Connection"
connectionString="provider=System.Data.SqlServerCe.4.0;provider connection string="Data source=|DataDirectory|\LocalDB.sdf""
providerName="System.Data.EntityClient" />
</connectionStrings>
but this gives me the following error:
但这给了我以下错误:
Some required information is missing from the connection string. The 'metadata' keyword is always required.
连接字符串中缺少一些必需的信息。始终需要'metadata'关键字。
4 个解决方案
#1
First Install this EntityFramework.SqlServerCompact nuget package and then set your connection string to somting like this:
首先安装此EntityFramework.SqlServerCompact nuget包,然后将您的连接字符串设置为somting,如下所示:
<connectionStrings>
<add name="connection" connectionString="Data Source=|DataDirectory|\TestDb.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
**Note:**By default entity framework will create TestDb.sdf if it doesn't exsist in you solutions \bin\Debug
directory and after deployment it will be in C:\Users\YourUserName\AppData\Roaming
this directory
**注意:**默认情况下,实体框架将创建TestDb.sdf,如果它在您的解决方案\ bin \ Debug目录中不存在,则在部署之后它将位于C:\ Users \ YourUserName \ AppData \ Roaming此目录中
#2
You are missing Server=(localdb)\v11.0
in your connection string to indicate that it is a LocalDB connection. In addition to that, you also have "
in your connection string that should be removed.
您在连接字符串中缺少Server =(localdb)\ v11.0以指示它是LocalDB连接。除此之外,您还应该删除应该删除的连接字符串。
Full details as to how your connection string should look like can be found on this MSDN page.
有关连接字符串应如何显示的详细信息,请参阅此MSDN页面。
Examples for connections strings can be found here (Scroll down to
可以在此处找到连接字符串的示例(向下滚动到
#3
I think that's a Sql Server Compact file, so your provider needs to be one of the System.Data.SqlServerCe providers.
我认为这是一个Sql Server Compact文件,因此您的提供程序需要是System.Data.SqlServerCe提供程序之一。
#4
<connectionStrings>
<add name="MyCS" connectionString="Data Source=|DataDirectory|\Database1.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
#1
First Install this EntityFramework.SqlServerCompact nuget package and then set your connection string to somting like this:
首先安装此EntityFramework.SqlServerCompact nuget包,然后将您的连接字符串设置为somting,如下所示:
<connectionStrings>
<add name="connection" connectionString="Data Source=|DataDirectory|\TestDb.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
**Note:**By default entity framework will create TestDb.sdf if it doesn't exsist in you solutions \bin\Debug
directory and after deployment it will be in C:\Users\YourUserName\AppData\Roaming
this directory
**注意:**默认情况下,实体框架将创建TestDb.sdf,如果它在您的解决方案\ bin \ Debug目录中不存在,则在部署之后它将位于C:\ Users \ YourUserName \ AppData \ Roaming此目录中
#2
You are missing Server=(localdb)\v11.0
in your connection string to indicate that it is a LocalDB connection. In addition to that, you also have "
in your connection string that should be removed.
您在连接字符串中缺少Server =(localdb)\ v11.0以指示它是LocalDB连接。除此之外,您还应该删除应该删除的连接字符串。
Full details as to how your connection string should look like can be found on this MSDN page.
有关连接字符串应如何显示的详细信息,请参阅此MSDN页面。
Examples for connections strings can be found here (Scroll down to
可以在此处找到连接字符串的示例(向下滚动到
#3
I think that's a Sql Server Compact file, so your provider needs to be one of the System.Data.SqlServerCe providers.
我认为这是一个Sql Server Compact文件,因此您的提供程序需要是System.Data.SqlServerCe提供程序之一。
#4
<connectionStrings>
<add name="MyCS" connectionString="Data Source=|DataDirectory|\Database1.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>