Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 首先使用不同的数据库对实体框架代码进行调试和发布(DbContext)_C#_Database_Entity Framework_Dbcontext_Localdb - Fatal编程技术网

C# 首先使用不同的数据库对实体框架代码进行调试和发布(DbContext)

C# 首先使用不同的数据库对实体框架代码进行调试和发布(DbContext),c#,database,entity-framework,dbcontext,localdb,C#,Database,Entity Framework,Dbcontext,Localdb,我已经使用创建了应用程序数据库,但现在我正在努力尝试创建两个独立的数据库,一个用于调试时,另一个用于发布,因为它没有为连接字符串添加任何app.config行 当我在服务器资源管理器中打开数据库的属性时,我可以看到实际的连接字符串是datasource=(localdb)\MSSQLLocalDB;初始目录=MediaManager.Model.Context;Integrated Security=True,但在调试或发布模式下,我无法区分它 我怎么做 App.config <?xml

我已经使用创建了应用程序数据库,但现在我正在努力尝试创建两个独立的数据库,一个用于调试时,另一个用于发布,因为它没有为连接字符串添加任何app.config行

当我在服务器资源管理器中打开数据库的属性时,我可以看到实际的连接字符串是
datasource=(localdb)\MSSQLLocalDB;初始目录=MediaManager.Model.Context;Integrated Security=True
,但在调试或发布模式下,我无法区分它

我怎么做


App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MediaManager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MediaManager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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.5" />
  </startup>
  <userSettings>
    <MediaManager.Properties.Settings>
      ...
    </MediaManager.Properties.Settings>
  </userSettings>
  <system.net>
    <defaultProxy useDefaultCredentials="true" />
  </system.net>
  <applicationSettings>
    <MediaManager.Properties.Settings>
      ...
    </MediaManager.Properties.Settings>
  </applicationSettings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

...
...

Context.cs

using System.Data.Entity;

namespace MediaManager.Model
{
    public class Context : DbContext, IContext
    {
        public virtual DbSet<Episodio> Episodio { get; set; }

        public virtual DbSet<Serie> Serie { get; set; }

        public virtual DbSet<SerieAlias> SerieAlias { get; set; }

        public virtual DbSet<Feed> Feed { get; set; }
    }
}
使用System.Data.Entity;
名称空间MediaManager.Model
{
公共类上下文:DbContext、IContext
{
公共虚拟数据库集情节{get;set;}
公共虚拟数据库集序列{get;set;}
公共虚拟数据库集序列IAS{get;set;}
公共虚拟数据库集提要{get;set;}
}
}

您应该能够简单地更新
上下文
以传入连接字符串的名称,并在
web.config
中定义此连接字符串

例如:

public class Context : DbContext, IContext
{
    public Context() : base("MyContext") { }

    public virtual DbSet<Episodio> Episodio { get; set; }

    public virtual DbSet<Serie> Serie { get; set; }

    public virtual DbSet<SerieAlias> SerieAlias { get; set; }

    public virtual DbSet<Feed> Feed { get; set; }
}

一旦设置好,您就可以使用来自动更新发布版的连接字符串,而不是调试版的连接字符串。

您不能在app.config文件中有两个不同的字符串,然后使用#if Debug来设置连接字符串吗?环境必须由开发、暂存和生产设置。不确定您使用的是哪个版本的MVC,但可以通过此链接[@anoop-我只需点击链接即可证明-热链接完全没有用,(链接现在已失效)请将答案放入评论或在将来提供答案。。
<configuration>
    <connectionStrings>
        <add name="MyContext" connectionString="..your connection string.." providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>