locked
How EF will get the system generated ID of a newly added object, and assign it to another object RRS feed

  • Question

  • User-540818677 posted

    I am working on an asp.net mvc5 web application + i am using EF6.0. and sometimes i can not understand how EF handles things internally. for example i have two entities named; Technology & Rack. where the Technology.TechnologyID is a system generated ID , while the Rack.RackID is user defined key and will be = to the Technology.TechnologyID. .so i have the following method to add a new technology , then to add a new rack and assign the Rack.RackID to be equal to the system generated Technology.TechnologyID.

      Technology technology = new Technology
                        {
                            IsCompleted = false,
                            IsDeleted = true,
                            TypeID = technologyypeID,
                            Tag = "CAB" + max.ToString(),
                            PartialTag = max,
                            StartDate = DateTime.Now,
                            IT360ID = dbresource.RESOURCEID,//,,
                            IsManaged = rj.Technology == null ? true : rj.Technology.IsManaged
    
    
                        };
    
    
    
                        rj.Rack.RackID = technology.TechnologyID;
    SAVE():

    now at the beginning i though that i above will not work , since when i try:-

    rj.Rack.RackID = technology.TechnologyID;

    the TechnologyID will not be available at this time, and using VS i found that at run time the TechnologyID will be equal to zero :-

    enter image description here

    so i am not sure how EF handle this ? and it force the Rack.RackID to be equal to the Technology.TechnologyID , although at run-time the technology-id will be zero. here is the sql statement that have been generated from the sql profiler :-

    exec sp_executesql N'insert [dbo].[Racks]([RackID], [DataCenterID], [ZoneID], [IT360SiteID])
    values (@0, @1, @2, @3)
    select [timestamp]
    from [dbo].[Racks]
    where @@ROWCOUNT > 0 and [RackID] = @0',N'@0 int,@1 int,@2 int,@3 bigint',@0=128,@1=3,@2=2,@3=4

    not sure how the 128 was assigned to the RAckID although at run time the value was zero?

    Friday, July 31, 2015 7:10 AM

Answers

  • User-84896714 posted

    Hi johnjohn123123,

    Thank you for your post.

    johnjohn123123

    so no KEY is being added to the model classe.. and the incremental is done the DB.

    If a column is set to Identity(auto generated) in Database, Entity Framework set it's StoreGeneratedPattern property to Identity.
    https://msdn.microsoft.com/en-us/library/system.data.metadata.edm.storegeneratedpattern(v=vs.110).aspx

    johnjohn123123

    but i did not do so , and the 2 insert statements work well,, this is my question...

    Entity Framework is a ORM(Object-relational mapping) framework. It allow us easily acess database. Before we invoke context.SaveChanges() method. All your behaviors happens  in memory on client side. In client memory, It will store some virtual tables, every row in the tables has a state. After you invoke SaveChanges method, it will affect database base on state of each row.

    In addition, Entity Framework is a open source project, you could down the source to check it out.
    https://entityframework.codeplex.com/

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2015 3:14 AM

All replies

  • User2024324573 posted

    Entity framework with Code-First approach, the ID(Identity) values for a model class is defined when you annotate with [Key] attribute in the property declaration. It enforces the EF to increase the ID by 1 when a new insert happens.

    In your code, you need a return ID (TechnologyID) from Technology table after any insert (as far as I understood), then after the insert just get the value for put it to the second insert which is Rack.

    rj.Rack.RackID = technology.TechnologyID;

    please use SAVE() before the line, as you will not get the return value until it is being saved.

    Hope this will help.

    Friday, July 31, 2015 7:20 AM
  • User-540818677 posted

    Entity framework with Code-First approach, the ID(Identity) values for a model class is defined when you annotate with [Key] attribute in the property declaration. It enforces the EF to increase the ID by 1 when a new insert happens.

    i am using Database first not code first approach. so no KEY is being added to the model classe.. and the incremental is done the DB.

    In your code, you need a return ID (TechnologyID) from Technology table after any insert (as far as I understood), then after the insert just get the value for put it to the second insert which is Rack.

    but i did not do so , and the 2 insert statements work well,, this is my question...

    please use SAVE() before the line, as you will not get the return value until it is being saved.

    but i am not facing problems in my case,,, and this is my question. so you make me more confused .. you are saying that i need to use SAVE before the line ,,, but i am not doing so and EF retrieved the system generated id and assign it to the RackID .. my question is how this magic happened ? and is this a guaranteed behavior all the time ,, and under high load ???

    Friday, July 31, 2015 7:40 AM
  • User-84896714 posted

    Hi johnjohn123123,

    Thank you for your post.

    johnjohn123123

    so no KEY is being added to the model classe.. and the incremental is done the DB.

    If a column is set to Identity(auto generated) in Database, Entity Framework set it's StoreGeneratedPattern property to Identity.
    https://msdn.microsoft.com/en-us/library/system.data.metadata.edm.storegeneratedpattern(v=vs.110).aspx

    johnjohn123123

    but i did not do so , and the 2 insert statements work well,, this is my question...

    Entity Framework is a ORM(Object-relational mapping) framework. It allow us easily acess database. Before we invoke context.SaveChanges() method. All your behaviors happens  in memory on client side. In client memory, It will store some virtual tables, every row in the tables has a state. After you invoke SaveChanges method, it will affect database base on state of each row.

    In addition, Entity Framework is a open source project, you could down the source to check it out.
    https://entityframework.codeplex.com/

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2015 3:14 AM