none
Identity column and Entity Framework RRS feed

  • Question

  • Hi All,

    I defined an identity column in a database table which also happens to be primary key (non-null). I am trying to use entity framework to insert/update new rows in this table. Visual studio is forcing me to provide value to identity column which I do not want to supply and want to let database do its job. How can I implement this?

    Thank you!

    Tuesday, February 25, 2014 11:05 PM

Answers

  • Got this to work!

    My entity name is DB_Table so up until now I was using the API, DB_Table.CreateDB_Table() which was expecting 5 parameters including the identity column. I changed it to what Fred suggested: DB_Table new = new DB_Table() { .......... }

    and this worked!

    • Marked as answer by diffident Wednesday, February 26, 2014 4:31 PM
    Wednesday, February 26, 2014 4:31 PM

All replies

  • Hello,

    >> Visual studio is forcing me to provide value to identity column which I do not want to supply and want to let database do its job.

    It seems that you are using database first. What the database and Entity Framework version do you use? How do you define the table? And could you please share how it force you to provide value to identity column?

    As far as I know, we do not need to provide value to an identity primary key even the primary key is composite primary key.

    Here I made an example with SQLServer2012, VS2012, .NET Framework 4.5 and Entity Framework 5:

    My table:

    CREATE TABLE [dbo].[BeerTable] (
    
        [RecordID] INT           IDENTITY (1, 1) NOT NULL,
    
        [Product]  NVARCHAR (50) NOT NULL,
    
        [BrewDate] DATETIME      NOT NULL,
    
        [Batch]    INT           NOT NULL,
    
        PRIMARY KEY CLUSTERED ([Product] ASC, [RecordID] ASC)
    
    );
    

    Then to  add a record as:

    BeerTable b = new BeerTable() { Product = "1", BrewDate = new DateTime(2012, 01, 01), Batch = 1 };
    
    
                    db.BeerTables.Add(b);
    
    
                    db.SaveChanges();
    

    And it generated the RecordID automatically in databse. You can have a try.

    If you want to know more, please refer to link below:

    http://msdn.microsoft.com/en-us/library/vstudio/dd283139(v=vs.100).aspx

    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.

    Wednesday, February 26, 2014 5:53 AM
    Moderator
  • Thanks for your reply Fred.

    I am using SQL Server 2012 and I am using v3.5 of System.Data.Entity.dll. Is that the version of Entity Framework?

    Yes, I am using an existing database table and trying to populate it using Entity Framework API and C#. I have an identity column in database table and I am getting an error in C# that says that it expects 5 parameters and not 4 paramaters while creating an object of type (database table). That extra parameter that it expects is the identity column.

    I have set StoreGeneratedPattern to None and tried populating but same result.

    Any pointers?

    Wednesday, February 26, 2014 3:07 PM
  • Got this to work!

    My entity name is DB_Table so up until now I was using the API, DB_Table.CreateDB_Table() which was expecting 5 parameters including the identity column. I changed it to what Fred suggested: DB_Table new = new DB_Table() { .......... }

    and this worked!

    • Marked as answer by diffident Wednesday, February 26, 2014 4:31 PM
    Wednesday, February 26, 2014 4:31 PM