本文介绍了如何编程设置数据库文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本教程,我创建了我的 DbConfiguration 类:

class ImgSigDbConfig : DbConfiguration
{
    public ImgSigDbConfig()
    {
        SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
    }
}

和告诉我的的DbContext 来使用它:

[DbConfigurationType(typeof(ImgSigDbConfig))]
class ImgSimContext : DbContext
{

这似乎正常连接,但我不知道它的存储我数据,我不喜欢这样。我想这一切保存到App_Data文件夹中的文件。我怎么可以指定从我的 ImgSigDbConfig 构造函数中? (假设我什么都不知道SQL服务器连接)

Which seems to connect fine, but I have no idea where it's storing my data, and I don't like that. I would like it to save everything to a file in the App_Data folder. How can I specify that from inside my ImgSigDbConfig constructor? (Assume I know nothing about SQL server connections)

做了一些更多的挖掘以及与此想出了:

Did some more digging and came up with this:

public ImgSigDbConfig()
{
    var sqlConnBuilder = new SqlConnectionStringBuilder();
    sqlConnBuilder.DataSource = ".";
    sqlConnBuilder.InitialCatalog = "ImgSim.ImgSimContext";
    sqlConnBuilder.IntegratedSecurity = true;
    sqlConnBuilder.AttachDBFilename = @"App_Data\database.mdf";

    SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0", sqlConnBuilder.ToString()));
}



但现在它抛出:

But now it throws:

提供程序未返回ProviderManifestToken字符串

每哈伊姆和冈瑟的建议我删除多余的连接设置:


Per haim and gunther's suggestions I removed the superfluous connection settings:

class ImgSigDbConfig : DbConfiguration
{
    public ImgSigDbConfig()
    {
        var sqlConnBuilder = new SqlConnectionStringBuilder();

        sqlConnBuilder.IntegratedSecurity = true;
        sqlConnBuilder.MultipleActiveResultSets = true;

        SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0", sqlConnBuilder.ToString()));
        SetManifestTokenResolver(new DefaultManifestTokenResolver());
    }
}

和感动他们到Program.cs的:

And moved them into Program.cs:

var conn = new SqlConnectionStringBuilder
{
    AttachDBFilename = @"App_Data\database.mdf",
    //InitialCatalog = "ImgSim.ImgSimContext",
};

using (var db = new ImgSimContext(conn.ToString()))

但即便如此,我收到了 System.Data.Entity.Core.ProviderIncompatibleException 用的InnerException:

But even so, I get a System.Data.Entity.Core.ProviderIncompatibleException with InnerException:

提供者未返回ProviderManifestToken字符串。

尽管我设置了 SetManifestTokenResolver - 我想一个默认不起作用?

Even though I set a SetManifestTokenResolver -- I guess the default one doesn't work??

推荐答案

LocalDbConnectionFactory构造V11.0允许连接到SQLSERVER 2012,默认情况下它试图找到的app.config为背景的全名在连接字符串的名称。说你没有指定在配置连接字符串,并希望创建一个名为test数据库(在下面指定DataDirectory目录),那么你需要设置这样的构造:

LocalDbConnectionFactory constructor v11.0 allows to connect to sqlserver 2012 and by default it tries to find the name of the connection string in app.config as context full name. Say you have not specified the connection string in config and want to create a database named Test ( In a datadirectory specified below) then you need to set the constructor like this:

   [DbConfigurationType(typeof(ImgSigDbConfig))]
class MyDbContext : DbContext
{
    public MyDbContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
    }

    public DbSet<Person> Persons { get; set; }
}

现在,你可以调用上下文:

Now you can call the context :

AppDomain.CurrentDomain.SetData("DataDirectory", @"c:\App\DataDirectory");
        MyDbContext myDbContext = new MyDbContext("test"); //this is the name
        myDbContext.Persons.Add(new Person() { Id = 1, Name = "Name1" });
        myDbContext.SaveChanges();



该配置将保持不变:

The configuration will remain same:

class ImgSigDbConfig : DbConfiguration
{
    public ImgSigDbConfig()
    {
        SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
    }
}

手表,我手动设置DataDirectory目录,因为它是不是一个Web应用程序。你需要给权限,这个目录,这样一个新的数据库可以创建和连接。
我的app.config是这样的:

Watch that I am setting the datadirectory manually as it is not a web app. You need to give permissions to this directory so that a new database can be created and attached.My app.config is like this:

    <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>  <connectionStrings>
  </connectionStrings>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

这篇关于如何编程设置数据库文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:37
查看更多