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!?

5
  • No primary key? No auto-generated identity column? Try adding INT id, identity, primary key to your table so that EF has an EntityKey. Commented May 15, 2010 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! Commented May 15, 2010 at 17:11
  • If you wrap this in a try....catch.... block - do you get any exceptions, and if so - what exceptions exactly?? Commented May 15, 2010 at 20:37
  • I don't get any exceptions... :{ Commented May 16, 2010 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... Commented May 16, 2010 at 11:37

4 Answers 4

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

2 Comments

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: stackoverflow.com/questions/1603472/…
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.
0

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

Comments

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);
}

Comments

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

Comments

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.