Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework change database physical location by class

I am new to entity framework. I wrote a simple code first database. how to change the physical location of database.mdf by class. let's say i what to save my database in |DataDirectory|\App_Data\database.mdf. I am using windows forms. here is the 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; }
}

and here is the configuration file 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>`

thanks for you time and please help. :)

like image 760
Etan Cole Avatar asked Sep 25 '22 19:09

Etan Cole


1 Answers

I would just declare another constructor on your database context where you send in the location of the database.

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

Notice the call to the base(), the base class constructor. According to EF Spec, you can pass in

  • An existing connection to a database
  • The name of a connection string contained in the .config
  • A connection string

Hope this helps.

like image 98
Bubba Avatar answered Sep 29 '22 05:09

Bubba