none
An error occurred while updating the entries. See the inner exception for details

    Question

  •  

    I followed this tutorial and wrote the following code:

     

    Module Module1
    
        Sub Main()
            Dim db As New PersonModelContainer
            Using db
    
                db.People.Add(New Person With {.FullName = "George1"})
                db.People.Add(New Person With {.FullName = "George2"})
                db.People.Add(New Person With {.FullName = "George3"})
                db.People.Add(New Person With {.FullName = "George4"})
                db.SaveChanges()
    
                Dim per As Person
                Dim pipl As IQueryable(Of Person)
                pipl = From p In db.People
                     Order By p.FullName
                     Select p
    
                Console.WriteLine("All People")
                For Each per In pipl
                    Console.WriteLine("- {0}", per.FullName)
                Next
    
                db.People.First.FullName = "Jan"
                db.SaveChanges()
    
                Console.WriteLine("Press any key to exit...")
                Console.ReadKey()
    
            End Using
        End Sub
    
    End Module

     

    I then get the following error:

    An error occurred while updating the entries. See the inner exception for details.

    The inner exception says the following:

    Default values not supported

    What am I doing wrong?

     

    Thanks for your help!

    Thursday, September 29, 2011 12:54 AM

Answers

  • I'm not sure why you don't have execute SQL , maybe it's because you aren't connected to the database file.  Go to your database explorer and see if you can create a connection to your database file.

    But long term I would recommend you install sql server management studio (SSMS), it's very useful for connecting to a sql server or sql server CE database and being able to run queries on it and view the database objects.  The VS interface for working with databases isn't very good.

    http://www.microsoft.com/download/en/details.aspx?id=7593

    One thing I noticed is the auto generated code by entity framework doesn't create an identity column on your People table.  You probably want to do that.  So change the create statement code that's in your window to this:

    CREATE TABLE [People] (
        [Id] int identity NOT NULL,
        [FullName] nvarchar(4000)  NOT NULL
    );
    GO
    

    Larcolais Gong's database script also shows this.  However Larcolais's code is for full sql server, I don't know if it will work with your sql ce database.  I think you just need to modify your script and include the identity keyword like above. 

    Let me know if this works.  Seems like your code is working when Larcolais and myself try it, as long as there is a valid database and identity column in the table.

     

     

     


    Tom Overton
    • Marked as answer by lilypad49 Monday, October 03, 2011 9:24 PM
    Friday, September 30, 2011 1:59 PM

All replies

  • Hi lilypad,

    On what line of code does the error happen?  Is it on the first db.SaveChanges()?

    Check to see what your entity looks like in the designer.  Does it have an ID column (which should be your primary key)  and also a FullName column?  Make sure you created the entity just like the instructions in the tutorial and that it looks like what's on the screen shot.  If you only have a single column in your entity you may get an error.

    The person entity must have an ID column (which should be the one already there by default when you first created the entity in the designer) and also the FullName scalar property that you should have added yourself.

     

     


    Tom Overton
    Thursday, September 29, 2011 1:21 AM
  • Hi lilypad49;


    When you query the database you need to assign it to a variable then change its value and then save your changes. Change the following line of code from this :

    db.People.First.FullName = "Jan"
    db.SaveChanges()

    To this and see if that does it.

    Dim p As Person = db.People.First
    p.FullName = "Jan"
    db.SaveChanges()

    And as Tom stated the table must have a Primary key in the database table.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, September 29, 2011 3:32 AM
  • It does have an ID column. It also happens at the first db.SaveChanges

     

    FullName is a string.

    I actually have this problem also on a bigger model that I have, so I did this one just to figure out what I'm doing wrong. I followed the directions closely but to no avail. Not sure what I'm doing wrong.

    Thursday, September 29, 2011 6:05 AM
  • Sorry, I didn't specify, the error happens at the first db.SaveChanges. I went ahead and tried what you wrote, but it didn't work. Thanks for looking though. Any other ideas would be much appreciated.
    Thursday, September 29, 2011 6:11 AM
  • Hi lilypad49;

    I took a look at the tutorial that you were following and I went and followed the same steps and used your VB code and had no issues running it, all worked. If you would like and if you could put your project into a zip formated file on the web where I can download it I would look at it to see what the difference is.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, September 29, 2011 3:19 PM
  • I think it might be that your ID column is not setup as an Identity value in the database.  Go to your Person table in the database, and make sure the ID is setup as an Identity column.   Although more than likely you would get some message about "cannot insert nulls" if that was the case, but make sure anyway.


    Tom Overton

    • Edited by Tom_Overton Thursday, September 29, 2011 4:09 PM
    Thursday, September 29, 2011 4:01 PM
  • Tom, it's definitely an identity value.

     

    Fernando, I'll attach it. Also, I'm using VB.NET 2010 Express, could that be why it's not working? It should work on express too, right?


    How do you attach a file?

    Thursday, September 29, 2011 4:25 PM
  •  

    It should work on the Express version. As far as attaching the zip file you can NOT post it to this MSDN forum, you would need to use a work FTP site or if you are a member of Microsoft Windows Live you can set up a SkyDrive. Also Google has Google Docs. Just make sure you make them public and post the link here.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, September 29, 2011 4:43 PM
  • OK, here it is, thanks Fernando.
    Thursday, September 29, 2011 5:22 PM
  • Hi lilypad,

    I'm not sure if Fernando has had a chance to look at that yet, but  I wasn't able to reproduce your problem, after I pulled your project down and created a SQL CE database and generaed the People table using the DDL.

    The only thing I can think of to try, is delete the records in your people table, and try your code by passing it an ID besides the name and see if that makes a difference:

                db.People.Add(New Person With {.FullName = "George1", .Id = 1})
    
                db.People.Add(New Person With {.FullName = "George2", .Id = 2})
    
                db.People.Add(New Person With {.FullName = "George3", .Id = 3})
    
                db.People.Add(New Person With {.FullName = "George4", .Id = 4})

     Since the error is something about a default value, maybe go to your entity in the designer and take out the default value you have specified for FullName.  Although I really can't see that as being the cause.  But I'm running out of things to suggest!

    Can you also put your database file up there ? 


    Tom Overton
    • Edited by Tom_Overton Thursday, September 29, 2011 7:13 PM
    Thursday, September 29, 2011 7:05 PM
  • Hi Tom,


    I uploaded my database. I'll give your suggestions a try. I added the default value when I was trouble shooting, but I'll take it out. Also, I'm working on Windows XP, do you think that has anything to do with it?

    Thursday, September 29, 2011 7:39 PM
  • Tom, I tried your suggestions. Still getting the same error. Also, I'm using SQLCE 3.5, does it need to be newer version than 3.5?
    Thursday, September 29, 2011 7:53 PM
  • lilypad,

    Your database is blank, no tables in it?

    Maybe that's the problem?  Although i would think you would get a different error than what you are gettting. You would instead get an error like "cannot open source" or something like that.

    Do you have sql server management studio?  Download it if you don't have it, it's useful when working with the database.

    What you need to do, is run the DDL (data definition language) code in your PersonModel.edmx.sqlce file.

    The basic code you need to run is this:

    CREATE TABLE [People] (
        [Id] int  NOT NULL,
        [FullName] nvarchar(4000)  NOT NULL
    );
    GO
    
    
    ALTER TABLE [People]
    ADD CONSTRAINT [PK_People]
        PRIMARY KEY ([Id] );
    GO
    

     


    Tom Overton
    Thursday, September 29, 2011 8:01 PM
  • Oh, thanks Tom. So why, when you run the program, it creates the tables, but mine doesn't? How does this affect deployment (I thought it was supposed to automatically create a database with tables)?
    Thursday, September 29, 2011 8:34 PM
  • Well,  I didn't do it the automatic way, the way the tutorial said to do it.  I justed created a blank CE database, and then ran the SQL script on it that was already in your solution.  I'm not sure why when you followed the directions in the tutorial it didn't create a database and table for you.  Maybe try to do the part of the tutorial over again, where explains how to generate the database.  You just go to your designer, right click on it, and specify "Generate Database from Model". 
    Tom Overton
    Thursday, September 29, 2011 8:41 PM
  • OK, I guess that is what I was missing. This step:

    On the generated script; Right Click –> Execute SQL…

    Except I don't see that when I right click on the PersonModel.edmx.sqlce.

    Maybe I should ask a new question and mark yours as answering mine. Thanks for your help!

    Thursday, September 29, 2011 9:05 PM
  • What you have to do is first double click on the PersonModel.edmx.sqlce.  You will then see the contents of that file open in a window.  Then, you right click on window and you will see the option to Execute SQL.

    A pop up window will come up and ask you to specifiy a database file, or you can create a new one, and then it will run the script on the database file.

    When you first try to run the script you may get an error that it can't drop the table.  You may have to comment out the "drop table [people];"  , but i think the error is just a warning it will still create the table. 

     


    Tom Overton

     


    • Edited by Tom_Overton Thursday, September 29, 2011 9:26 PM
    Thursday, September 29, 2011 9:21 PM
  • When I right click this is what it shows (I'm not seeing the "execute sql") (I'm out for the rest of the day, so I'll have to work on this again tomorrow, thanks for your constant help!):



    • Edited by lilypad49 Thursday, September 29, 2011 10:07 PM
    Thursday, September 29, 2011 10:05 PM
  • Hello,

    I made a test on my side. It looks OK. I think this issue should be occurred database side. I attached some code snippets on my side.

    SET QUOTED_IDENTIFIER OFF;
    GO
    USE [Xueyun2];
    GO
    IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
    GO
    
    -- Creating table 'People'
    CREATE TABLE [dbo].[People] (
        [Id] int IDENTITY(1,1) NOT NULL,
        [FullName] nvarchar(max)  NOT NULL
    );
    GO
    
    -- Creating primary key on [Id] in table 'People'
    ALTER TABLE [dbo].[People]
    ADD CONSTRAINT [PK_People]
        PRIMARY KEY CLUSTERED ([Id] ASC);
    GO
    

    I suggest you delete your application and refactor it again. Please feel free to let us know how's going.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 30, 2011 2:36 AM
  • I'm not sure why you don't have execute SQL , maybe it's because you aren't connected to the database file.  Go to your database explorer and see if you can create a connection to your database file.

    But long term I would recommend you install sql server management studio (SSMS), it's very useful for connecting to a sql server or sql server CE database and being able to run queries on it and view the database objects.  The VS interface for working with databases isn't very good.

    http://www.microsoft.com/download/en/details.aspx?id=7593

    One thing I noticed is the auto generated code by entity framework doesn't create an identity column on your People table.  You probably want to do that.  So change the create statement code that's in your window to this:

    CREATE TABLE [People] (
        [Id] int identity NOT NULL,
        [FullName] nvarchar(4000)  NOT NULL
    );
    GO
    

    Larcolais Gong's database script also shows this.  However Larcolais's code is for full sql server, I don't know if it will work with your sql ce database.  I think you just need to modify your script and include the identity keyword like above. 

    Let me know if this works.  Seems like your code is working when Larcolais and myself try it, as long as there is a valid database and identity column in the table.

     

     

     


    Tom Overton
    • Marked as answer by lilypad49 Monday, October 03, 2011 9:24 PM
    Friday, September 30, 2011 1:59 PM