none
Entity Framework doesn't save new record into database RRS feed

  • Question

  • Hy,

    I have problem with saving new record into database using Entity Framework.

    When I run program, everything seems normal, without errors . Program shows existing, manually added records into the database, and new one too. But new one isn't save into database after running program.

    I've got no idea where's problem. There is code for add new record, show existing.

    Thanks for help!!

     // add new record
    using (var db=new DatabaseEntitiesContext())
                {
                    var person = new Table()
                    {
                        First_Name = "New_FName",
                        Second_Name = "New_SName",
                        PIN = "4569"
                    };
    
                    db.Tables.Add(person);
                    db.SaveChanges();
                }
    
    //show all records
                using (var db=new DatabaseEntitiesContext())
                {
                    var selected = from x in db.Tables
                        select x;
    
                    foreach (var table in selected)
                    {
                        Console.WriteLine("{0}{1}{2}",table.First_Name,table.Second_Name,table.PIN);
                    }
                }

    Sunday, January 19, 2014 9:34 PM

Answers

  • Hi BownieCross;

    The issue you are having is that the database file in the main project folder, this is a local database file, is copied over to the bin/debug folder each time you run the program and over writing the actual database file that is being used and so it looks as if the Entity Framework is not saving the data but in fact it is and is being over written when you re-run the program.

    Please see this link ==> How to: Manage Local Data Files in Your Project <== to find the three different options on how the copying of the database will be handled. 

    The project is currently setup to, "The database file is copied from the project directory to the /bin directory every time you build your application. Therefore, if you build your application and save changes to the file in the /bin directory, those changes are overwritten the next time that the original file is copied to the /bin directory.", this is happenning becaues the properties page of the database file in the main project folder is set to, "Copy Always", as shown in the image below. Please see the above link to see the other two options available and use the option that you prefer.

    Copy to Output Directory : Property of Database file


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by BownieCross Monday, January 20, 2014 2:53 PM
    Monday, January 20, 2014 2:23 PM

All replies

  • I suggest that you put a try/catch around the code to see if can exception is being thrown. 
    Monday, January 20, 2014 12:59 AM
  • Hi BownieCross;

    If you are using a local database file in your project the following may be the cause.

    From Microsoft Documentation:
    Issue:
    "
    Every time I test my application and modify data, my changes are gone the next time I run my application."
    Explanation:
    The value of the Copy to Output Directory property is Copy if newer or Copy always. The database in your output folder (the database that’s being modified when you test your application) is overwritten every time that you build your project. For more information, see How to: Manage Local Data Files in Your Project.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 20, 2014 2:58 AM
  • Hi It required the primary key, to insert in the table like this. Do you have one on your table?

    I don't even know how you built the model if using the model first approach. EF will not work if there is no primary-key on a table.

    The primary-key should be an Identity column on the table using Identity auto generated numbers. This means that when the entity/object is inserted into SQL Server by EF the key will be assigned by SQL Server. The key will be populated back to entity/object's primary-key property so that you can get the primary-key of the inserted entity/object off of the object when it's inserted.

    EF will take the following actions to persist the entity/object to the database table:

    1) If entity/object's primary-key property = 0, then EF will insert the object into the database.

    2) If entity/object's primary-key > 0, the EF will update the object on database table.

    Monday, January 20, 2014 3:23 AM
  • Hi Darnold,

    your point excepted, I will delete my; reply thanks.


    Regards, Preetam Ramdhave

    Monday, January 20, 2014 6:45 AM
  • With try/catch is the same thing. I tryed.

    Thanks for help

    Monday, January 20, 2014 9:04 AM
  • Hi,

    I'm using Service- based database added into project, with only one table.

    There's the table:

    CREATE TABLE [dbo].[Table] (
        [Id]          INT        IDENTITY (1, 1) NOT NULL,
        [First_Name]  NCHAR (10) NOT NULL,
        [Second_Name] NCHAR (10) NOT NULL,
        [PIN]         NCHAR (10) NOT NULL,
        CONSTRAINT [PK_Table] PRIMARY KEY CLUSTERED ([Id] ASC)

    Monday, January 20, 2014 9:07 AM
  • Hi,

    there's Service- based database added into project, with only one table. Just for  testing purposes. With auto incremet id wich's PK, and three other columns.

    So, there's code :

    CREATE TABLE [dbo].[Table] (
        [Id]          INT        IDENTITY (1, 1) NOT NULL,
        [First_Name]  NCHAR (10) NOT NULL,
        [Second_Name] NCHAR (10) NOT NULL,
        [PIN]         NCHAR (10) NOT NULL,
        CONSTRAINT [PK_Table] PRIMARY KEY CLUSTERED ([Id] ASC)

    Sorry for few information in my question.

    Thank's a lot.

    Monday, January 20, 2014 9:16 AM
  • Hello BownieCross,

    Do you have wrap an additional transaction outside like below:

    using (TransactionScope scope = new TransactionScope())
    
                {
    
                    using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
    
                    {
    
                        //…
    
                    }
    
                }
    

    If it is, we should also call scope.Complete() in using block to commit the transaction. And please share your program environment so that we can do a test or you can upload your program to:

    https://skydrive.live.com/

    We can download it to check why it does not work.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 20, 2014 10:22 AM
    Moderator
  • Hi Fred,

    TransactionScope doesn't help too. There is my soluition, please help.

    Best Regards.

    https://skydrive.live.com/redir?resid=5B8146585126B354%21107

    Monday, January 20, 2014 11:34 AM
  • Hi BownieCross;

    The issue you are having is that the database file in the main project folder, this is a local database file, is copied over to the bin/debug folder each time you run the program and over writing the actual database file that is being used and so it looks as if the Entity Framework is not saving the data but in fact it is and is being over written when you re-run the program.

    Please see this link ==> How to: Manage Local Data Files in Your Project <== to find the three different options on how the copying of the database will be handled. 

    The project is currently setup to, "The database file is copied from the project directory to the /bin directory every time you build your application. Therefore, if you build your application and save changes to the file in the /bin directory, those changes are overwritten the next time that the original file is copied to the /bin directory.", this is happenning becaues the properties page of the database file in the main project folder is set to, "Copy Always", as shown in the image below. Please see the above link to see the other two options available and use the option that you prefer.

    Copy to Output Directory : Property of Database file


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by BownieCross Monday, January 20, 2014 2:53 PM
    Monday, January 20, 2014 2:23 PM
  • Problem solved, thaks a lot Fernando.

    Best Regards!!

    Monday, January 20, 2014 2:55 PM
  •   

    Not a problem BownieCross, glad I was able to help.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 20, 2014 3:10 PM
  • Thanks a lot...this saves my day
    Thursday, February 5, 2015 8:35 AM
  • Hi Duke;

    If this post has helped you please think on voting on it.

    Thank you.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 5, 2015 3:36 PM