简体   繁体   中英

the provider did not return a ProviderManifestToken string in WPF using Code First Approach

I would like to know that what should I write to create my database because on running of my application, still no database is created. I want to create local database in Visual Studio 2013.

Here is my context class

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; }
}

Here is 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);
        }
    }
}

Here is connectionString

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

when I try to save some values in database table which is still not created,

exception occurs "the provider did not return a ProviderManifestToken string"

other Exception below in image 在此处输入图片说明

What should I do to solve this.

Change your connection string to this:

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

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