我正在使用VS2013 Ultimate創建WPF應用程序,其中我想在Visual Studio中創建本地數據庫。下面是我想寫如何使用EF5代碼優先方法在App.config文件中編寫connectionString
的ConnectionString在app.config文件
<connectionStrings>
<add name="RoznamchaContext"
connectionString="Server=.;database=sample;integrated security=true;"/>
</connectionStrings>
上下文類樣品的connectionString這裏
class RoznamchaContext : DbContext
{
public DbSet<Admin> Admins { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<Task> Tasks { get; set; }
public DbSet<Task_Tag> Task_Tags { get; set; }
public RoznamchaContext() : base("RoznamchaContext")
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
這是我的主類在那裏我有一個按鈕,當我按下按鈕時,它給了我一個圖像中顯示的異常,按鈕點擊事件也在下面給出。
public partial class MainWindow : Window
{
public MainWindow()
{
Database.SetInitializer<RoznamchaContext>(null);
InitializeComponent();
}
public static int count = 0;
private void btn_Click(object sender, RoutedEventArgs e)
{
try
{
RoznamchaContext context = new RoznamchaContext();
context.Tags.Add(new Models.Tag { PK_Tag = count, Name = "Tag" + count });
context.SaveChanges();
count++;
btn.Content = count.ToString();
}catch(Exception ex)
{ MessageBox.Show(ex.Message); }
}
}
您是否嘗試過在看這個http://www.connectionstrings.com/sql-server/ – Ruskin