使用 EF6 的 WPF 应用程序不会在 SQL Server Enterprise 中创建新数据库

WPF application with EF6 does not create new database in SQL Server Enterprise

我正在开发一个 WPF 应用程序,并且首先对数据库使用 entity framework 6 代码,但是当我 运行 该应用程序时,数据库没有在 SQL Server Enterprise 中创建。我在谷歌上搜索了很多但找不到任何合适的解决方案,我提前感谢大家。

下面是我的 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>

   <connectionStrings>
         <add name="DbConnectionString" connectionString="Data Source=.;Initial 
         Catalog=CricketAcademy;Integrated Security=True;" providerName="System.Data.SqlClient" />
   </connectionStrings>


   <startup> 
       <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
   </startup>
   <entityFramework>
       <providers>
           <provider invariantName="System.Data.SqlClient" 
           type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
       </providers>
   </entityFramework>
  </configuration>  

这是我的上下文 Class:

class Db : DbContext
{
    public Db() : base("name=DbConnectionString")
    {

    }
    public DbSet<Player> Players { get; set; }
    public DbSet<Player_Phone> PlayerPhones { get; set; }
    public DbSet<Admission> Admissions { get; set; }
    public DbSet<Fee> Fees { get; set; }
    public DbSet<Coach> Coaches { get; set; }
    public DbSet<Shift> Shifts { get; set; }

}  

这是我的主窗口 class:

public partial class MainWindow : Window
{
    private Db DB = new Db();
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

DbContext 的构造函数中添加:

public Db() : base("name=DbConnectionString")
{
      Database.SetInitializer<Db>(new CreateDatabaseIfNotExists<Db>());
}

您可以找到更多信息in this article