0

Okay, this is really weird. I made a simple database with a single table, Customer, which has a single column, Name. From the database I auto-generated an ADO.NET Entity Data Model, and I'm trying to add a new Customer to it like so:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main()
        {
            Database1Entities db = new Database1Entities();
            Customer c = new Customer();
            c.Name = "Harry";
            db.AddToCustomer(c);
            db.SaveChanges();
        }
    }
}

But it doesn't persist Customer "Harry" to the database! I've been scratching my head for a while now wondering why such a simple operation doesn't work. What on earth could be the problem!?

Dissonant
  • 11
  • 2
  • No primary key? No auto-generated identity column? Try adding INT id, identity, primary key to your table so that EF has an EntityKey. – Ian Mercer May 15 '10 at 17:03
  • Name was the primary key. But for your sake I tried with an ID column as the primary key as well and that didn't work either! – Dissonant May 15 '10 at 17:11
  • If you wrap this in a try....catch.... block - do you get any exceptions, and if so - what exceptions exactly?? – marc_s May 15 '10 at 20:37
  • I don't get any exceptions... :{ – Dissonant May 16 '10 at 11:37
  • Is there something I need to configure with SQL server on my computer to get it working? Like maybe its not enabled or something? It seems to connect to the database fine. And it will update/read from the database while the app is running, it's just that the data isn't persisted when the app is exited... – Dissonant May 16 '10 at 11:37

4 Answers4

1

EF requires that you have a unique index for many operations.

Try adding an identity field (primary key) to your table. Delete and recreate your model and try again.

Edit:

It looks like you are running this from a console app.

  • Do you have a connection string in the app.config file?
  • Do you have more than one project in your solution?
  • Are you getting any exceptions?

Edit2:

Next things to try:

  • Use SQL Server profiler to see what is being sent to the database
  • Open the EF model in an editor to see the xml, check if there are any errors
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • But Shiraz and Hightechrider are saying add an int column with Indentity = Yes as the primary key. It's a best practice and it's more optimized: http://stackoverflow.com/questions/1603472/indexing-performance-bigint-vs-varchar – JohnB May 15 '10 at 17:50
  • I know that it's best practice. This is just a simple database test. Please see my reply to Hightechrider. And to answer your questions, Shiraz: Yes. No. No. – Dissonant May 16 '10 at 11:35
0

Make sure that the "Copy to Output Directory" property of the database file is not set to "Copy always." If it is, every time you rebuild the application, the database may be clobbered with a pristine copy.

See: Entity Framework not flushing data to the database

Community
  • 1
  • 1
Joel Finkel
  • 116
  • 1
  • 7
0

Place db in a using statement to ensure the connection/transaction is closed cleanly before process exit.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

OK, here's a longshot. Are you sure your connection string is pointing to the right place? And the connection is functioning?

You can verify it by using SQL Server Management Studio to add some records to your test database, and then do something like

foreach (Customer c in db.Customers)
{
    Console.WriteLine(c.Name);
}
LSpencer777
  • 1,769
  • 2
  • 13
  • 14