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

1

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

I have made changes according to your suggested code. please go through the question again now I updated it with details.
Thank you Vadim, Thank you so much. errors are vanishing with the creation of new errors ;-) --- Now I have added Database.SetInitializer<RoznamchaContext>(null); in my MainWindow class constructor and having the error, please look at the updated question
exception comes when it submits the SaveChanges() method
@WASIF it's more generic problem. There is a detailed answer for your current problem here: stackoverflow.com/questions/2475008/… 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: codeproject.com/Tips/126919/…
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' "
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.