在entityFramework中设置附加参数sqlServerCe.3.5

时间:2017-10-25 23:48:32

标签: entity-framework-6 sql-server-ce

如何在app.config中设置其他参数以反映要创建数据库的目录?这个帖子" EF 5 + SQL Server CE 4: How to specify custom location for database file?"以编程方式进行,但我想在.config文件中设置参数。

这是我试图做的,但未能让它发挥作用:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
    <parameters>
        <parameter value="System.Data.SqlServerCe.3.5" />
        <parameter value="" />
        <parameter value="C:\\Users\\ericq\\Documents\\Data" />
    </parameters>
</defaultConnectionFactory>

甚至尝试过:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
    <parameters>
        <parameter value="System.Data.SqlServerCe.3.5" />
        <parameter value="data source=C:\\Users\\ericq\\Documents\\Data" />
    </parameters>
</defaultConnectionFactory>

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
    <parameters>
        <parameter value="System.Data.SqlServerCe.3.5" />
        <parameter value="data source=C:\Users\ericq\Documents\Data" />
    </parameters>
</defaultConnectionFactory>

并且,无法在MSDN上找到任何示例&#34; SqlCeConnectionFactory Constructor (String, String, String)&#34;

所以我假设您需要三个参数来完成这项工作,但我的格式根据LINQPad5不起作用,因为我尝试使用它来弄清楚如何使其工作:

  

ArgumentException初始化字符串的格式不符合从索引43开始的规范。

我使用的代码:

using (var db = new BloggingContext())
{
    Blog mMenu = (from p in db.Blogs where p.Name.Equals("Eric Miller") select p).FirstOrDefault();

    if (mMenu == null)
    {
        // Create and save a new Blog
        var name = "Eric Miller";
        var blog = new Blog { Name = name };
        db.Blogs.Add(blog);
        db.SaveChanges();
    }

    // Display all Blogs from the database
    var query = from b in db.Blogs
                orderby b.Name
                select b;

    Console.WriteLine("All blogs in the database:");

    foreach (var item in query)
    {
        Console.WriteLine(item.Name);
    }
}

当我这样做时会触发异常:

Blog mMenu = (from p in db.Blogs where p.Name.Equals("Eric Miller") select p).FirstOrDefault();

LINQPad5 App.config(我输入它吧:)

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.3.5" />
        <parameter value="" />
        <parameter value="C:\\Users\\ericq\\Documents\\Data" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SqlServerCe.3.5" type="System.Data.Entity.SqlServerCompact.Legacy.SqlCeProviderServices, EntityFramework.SqlServerCompact.Legacy" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.3.5" />
      <add name="Microsoft SQL Server Compact Data Provider 3.5" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>
</configuration>

谢谢!......

1 个答案:

答案 0 :(得分:0)

只需在您的配置中添加一个connectionString,其名称与您的上下文相同:

<connectionStrings>  
  <add name="BloggingContext"   
   providerName="System.Data.SqlServerCe.3.5"   
   connectionString="data source=C:\data\eric.sdf;" />  
</connectionStrings>