提供程序未使用Code First Approach在WPF中返回ProviderManifestToken字符串

时间:2017-02-23 05:25:19

标签: c# wpf entity-framework-5

我想知道我应该写什么来创建我的数据库,因为在运行我的应用程序时,仍然没有创建数据库。我想在Visual Studio 2013中创建本地数据库。

这是我的上下文类

class dbContext : DbContext
{
    public dbContext()
    {
        Database.SetInitializer<dbContext>(new DropCreateDatabaseIfModelChanges<dbContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
    }

    public DbSet<Employee> Employee { get; set; }
    public DbSet<Credit> Credit { get; set; }
    public DbSet<Debit> Debit { get; set; }
    public DbSet<Salary> Salary { get; set; }
    public DbSet<Category> Category { get; set; }
}

这是Main-Class

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        Database.SetInitializer<dbContext>(null);
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            dbContext context = new dbContext();
            Category c = new Category();
            c.Id = 1;
            c.Name = "Category";
            c.TotalExpenses = 0;

            context.Category.Add(c);
            context.SaveChanges();
            btn.Content = context.Category.SingleOrDefault(x => x.Id == 1);
        }
        catch (Exception ex)
        {
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;
                MessageBox.Show(ex.Message);
            }
            MessageBox.Show(ex.Message);
        }
    }
}

这是connectionString

<connectionStrings>
    <add name="dbContext"
         connectionString="server=.; database=sample; Integrated Security=true"
         providerName="System.Data.SqlClient"/>
</connectionStrings>

当我尝试在数据库表中保存一些仍未创建的值时,

  

发生异常“提供程序未返回ProviderManifestToken   字符串“

     

图片中的其他例外情况   enter image description here

我该怎么做才能解决这个问题。

1 个答案:

答案 0 :(得分:1)

将您的连接字符串更改为:

<connectionStrings>
<add name="dbContext"
     connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=sample;Integrated Security=SSPI" providerName="System.Data.SqlClient""/>