我想知道我应该写什么来创建我的数据库,因为在运行我的应用程序时,仍然没有创建数据库。我想在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 字符串“
我该怎么做才能解决这个问题。
答案 0 :(得分:1)
将您的连接字符串更改为:
<connectionStrings>
<add name="dbContext"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=sample;Integrated Security=SSPI" providerName="System.Data.SqlClient""/>