简体   繁体   中英

DropCreateDatabaseAlways in App.config file for EntityFramework 6.0 with Sql Server Ce

I have no idea, where should I set up System.Data.Entity.DropCreateDatabaseAlways parameter in my App.config file.

I do understand, that config section named "entityFramework" is defined here. I presume this section should be used. But I can't find any example of this type App.config file.

I'm using EF6.0 and SqlServer Compact.

<?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" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>
</configuration>

You can set the database initializer either within your DbContext class or in your web.config file. Both examples follow:

DbContext

public class YourDBContext : DbContext 
{
    public YourDBContext() : base("connstr") 
    {
        Database.SetInitializer<YourDBContext>(new DropCreateDatabaseAlways<YourDBContext>());
    }
    ...
}

App.config / web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="DatabaseInitializerForType Namespace.YourDBContext, Assembly"
             value="System.Data.Entity.DropCreateDatabaseAlways`1[[Namespace.YourDBContext, Assembly]], EntityFramework" />
    </appSettings>
</configuration>

See Database Initialization Strategies in Code-First .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM