c# - Entity Framework 按类更改数据库物理位置

标签 c# entity-framework code-first

我是 Entity Framework 的新手。我写了一个简单的代码优先数据库。 如何按类更改database.mdf 的物理位置。假设我要在 |DataDirectory|\App_Data\database.mdf 中保存我的数据库。我正在使用 Windows 窗体。 这是 DbContext

 class DatabaseContext : DbContext
{
    public DatabaseContext()
        : base("database")
    { }
    public DbSet<Login> Logins { get; set; }
    public DbSet<master> masters { get; set; }
    public DbSet<People> People { get; set; }
}

这里是配置文件app.config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- 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.2" />
  </startup>
  <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>`<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- 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.2" />
  </startup>
  <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>`

感谢您的宝贵时间,请提供帮助。 :)

最佳答案

我只是在您发送数据库位置的数据库上下文中声明另一个构造函数。

public DatabaseContext(string connection) : base(connection)  { }

注意对基类构造函数 base() 的调用。 根据EF Spec , 你可以传入

  • 与数据库的现有连接
  • .config 中包含的连接字符串的名称
  • 一个连接字符串

希望这对您有所帮助。

关于c# - Entity Framework 按类更改数据库物理位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34521597/

相关文章:

sql - 配置文件 Entity Framework

c# - Entity Framework - 外键组件......不是类型的声明属性

asp.net-mvc-2 - 部署 SQLCE.EntityFramework 4.0.8435.1

C# ASP.NET Core 2.1/ Entity Framework : Entity collention dosen't saving

c# - T4 - Entity Framework 错误 : Method not found: 'System.Data.Entity.DbSet` 1

asp.net-mvc - 生成 <DbContext> 时出错

c# - 重载时为什么不考虑函数的返回类型?

c# - 为什么无法在 asp.net 中获取正确的下拉列表值

c# - 如何使用脚本创建xml文本并在场景中显示文本?

c# - 检测 ActiveSelf 的事件