I am creating WPF application using VS2013 Ultimate in which I want to create local database in Visual Studio. Here is sample connectionString which I am trying to write
ConnectionString in App.config file
<connectionStrings>
<add name="RoznamchaContext"
connectionString="Server=.;database=sample;integrated security=true;"/>
</connectionStrings>
Context Class is here
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>();
}
}
This is my main class where I have a button, and when I press the button it gave me an exception shown in an image and button click-event is also given below.
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); }
}
}
This image shows exception thrown by click-event named "btn_Click"