Linq to Entities Question RRS feed

  • Question

  •  hi

    I have a Business Table in my DB. it has a BusinessID as its PK with SQl auto Increment. 

    All works fine however i am confused about something.


     Business NewBusiness = new Business();
            NewBusiness.Name = BusinessNameTxt.Text;

            BusinessActions Ba = new BusinessActions();

    NewBusiness.BusinessID  // it will give me the auto number. It knows this before i insert the record, how does this work ?.

    what happnes if i 5 webpages eatch inserting a record at the same time. will linq to entites not get duplicate numbers ?

    this is a interesting question.




    Tuesday, May 5, 2009 8:05 PM

All replies

  • If this is entity objects, just instantiating a new object in memory will not populate the BusinessID field unless you have some custom business logic that you have implemented.

    However, if you instantiate a new NewBusiness object, attache it to an objectCnotext and then call SaveChanges, the default behavior of EF is that it will bring back the new BusinessID created by the database and insert it into the NewBusiness object. In this case, you don't have a conflict because it is the database creating the ids and when you do a Linq to Entities query it will get the ids from the database.

    Does that make sense? It sounds a little convoluted I might need to try to express it a bit differently. :-)


    • Proposed as answer by JollyWagoner Sunday, November 20, 2011 9:14 PM
    Wednesday, May 6, 2009 12:19 AM
  • Hi Gary,

    I would only add my two cents to what Julie said that this what EF do it for you.You need to see how EF generates Primary Key for the auto-increment field that is set in your db.





    Sunday, November 20, 2011 9:15 PM