0

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" enter image description here

WasiF
  • 26,101
  • 16
  • 120
  • 128

1 Answers1

1

Your connection string is almost right. If you are using Entity framework you should also add providerName to your configuration:

<connectionStrings>
  <add name="RoznamchaContext" 
       connectionString="Server=.;database=sample;integrated security=true;"
       providerName="System.Data.SqlClient" />
</connectionStrings>

Now you can use connection string name as a parameter of DbContext:

class YourContext : DbContext
{
    public YourContext() 
        : base("RoznamchaContext")
    { }
}

You can also use convention over configuration when you named your connection string with the same name as your data context. Then you can use default DbContext constructor (without parameters):

class RoznamchaContext : DbContext
{
}

<add name="RoznamchaContext" 
     connectionString="Server=.;database=sample;integrated security=true;"
     providerName="System.Data.SqlClient" />

UPDATE

The other solution is to specify database initializer on the app Startup:

// add next line to the Run() method or Main method or other initialization method
Database.SetInitializer<RoznamchaContext>(null);
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • I have made changes according to your suggested code. please go through the question again now I updated it with details. – WasiF Jan 25 '16 at 08:32
  • Thank you Vadim, Thank you so much. errors are vanishing with the creation of new errors ;-) --- Now I have added Database.SetInitializer(null); in my MainWindow class constructor and having the error, please look at the updated question – WasiF Jan 25 '16 at 11:00
  • exception comes when it submits the SaveChanges() method – WasiF Jan 25 '16 at 11:04
  • @WASIF it's more generic problem. There is a detailed answer for your current problem here: http://stackoverflow.com/questions/2475008/error-the-underlying-provider-failed-on-open The short way is to try add `context.Connection.Open();` to your code. The other one solution is to change some security setting like in this article: http://www.codeproject.com/Tips/126919/Solution-for-The-underlying-provider-failed-on-Ope – Vadim Martynov Jan 25 '16 at 11:11
  • One thing I correct through these provided links is I was using "System.Data.SqlClient" then I made change to "System.Data.EntityClient". Now it is saying "keyword not supported 'server' " – WasiF Jan 25 '16 at 11:35
  • Nooooo EntityClient used with EDM. Why are you changed it? Is there an error with SqlClient and Database.SetInitializer? – Vadim Martynov Jan 25 '16 at 12:46