2

I have a C# winforms application, and a SQL Server .mdf database file.

I tried to use Entity Framework. It works great when retrieving data from the database, but on calling .SaveChanges() nothing happens. No error, no exception, and no changes saved.

This issue just drives me crazy. I found a bunch of questions with nearly the same issue, but neither of the answers apply to my case.

My code looks like that:

using (World_ParkingEntities context = new World_ParkingEntities())
{
   client _client = context.CreateObject<client>();
   _client.name = name;
   _client.mobile_number = phoneNo;
   _client.email = email;
   context.AddToclients(_client);
   context.SaveChanges();
}

My connection string:

<add name="World_ParkingEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\World_Parking.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
4
  • Read my comment stackoverflow.com/questions/21215864/… Commented Jan 19, 2014 at 13:06
  • I saw a lot of comment from you... which exactly one you meant? Commented Jan 19, 2014 at 13:12
  • 1
    Can you please show us your connection string? Also: when you add the entity using .AddToClients(), then there's really no need to do the next two lines (getting entity state and manually setting it to Added - the entity state will already be Added anyway!) Commented Jan 19, 2014 at 13:22
  • You are right, I also thought that this line is unnecessary, I added it as part of my trials. I edited the question above, please review Commented Jan 19, 2014 at 13:35

1 Answer 1

3

The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint after the context.SaveChanges(); call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. World_Parking)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=World_Parking;Integrated Security=True
    

    and everything else is exactly the same as before...

Sign up to request clarification or add additional context in comments.

2 Comments

Does it mean that my DB will be located locally on the server I worked on?
To use .mdf you need a SQL Server instance anyway - no other way to do this. And it would be a lot easier to actually create the database on that server instance upfront, instead of using the flawed (and deprecated) AttachDbFileName= approach

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.