none
Is it possible to write data with Linq? RRS feed

  • Question

  • Hi,

      I have this code to populate one table (Reservs).

          ModelEntities ent = new ModelEntities();
          WS_RESERVES reserve = new WS_RESERVES();
          reserve.CLIENTS = new CLIENTS() { CLIENT = 1 };
          reserve.PRODS = new PRODS() { PROD = 1 };
          reserve.QTD = 1;
          reserve.PRICE = 23;
          reserve.WS_STATUS = new WS_STATUS() { STATUS = 1 };
          ent.AddToWS_RESERVES(reserve);
          ent.SaveChanges(true);

      Seem to be ok, but it is not. I can't save it because I didn't informed the cities of the clients, the kind of the product and many others relashionship the these tables have.

      How can I save data in Reservs table? Is it so complex?

      Thanks,  
      Marco Castro
    Friday, March 19, 2010 8:58 PM

Answers

  • Hello Marco,

     

    Welcome to MSDN Forums!

     

    From your sample codes, I think you are using Entity Framework instead of LINQ to SQL, so I will move this thread to EF forum. 

     

    Based on my understanding, you want to directly set the foreign key column of the Reserve table, right?    It seems that the CLIENT, PRODS, WS_STATUS all exist in the database.   In EFv1 (.NET 3.5 SP1), I have three methods for your references:

     

    1.      Retrieve the entities from the database and set them to the navigation properties of the Reserve entity.  In this method, there will be several database calls to retrieve the existing entities.  So if you don’t want first retrieve these entities, please refer to the other two workarounds.

     

    2.      Create certain entities with only the entity key (Primary Key) and attach these entities to the ObjectContext.  Then we can set these entities to the navigation properties of the Reserve entity.   EF tracks the entity via their entity keys.  After we attach the entities, EF will treat them as existing entities in the database.   

    ======================================================================================

                using (TestDBEntities context = new TestDBEntities())

                {

                    ChildTable c = new ChildTable();

                    c.ID = 1;

                    c.Name = "Child";

                    ParentTable p = new ParentTable() { ID = 1 };

                    context.AttachTo("ParentTable", p);

                    c.ParentTable = p;

                    context.AddToChildTable(c);

                    context.SaveChanges();

         }

    ======================================================================================

     

    3.      Set the EntityReference.EntityKey directly.  

    ======================================================================================

                using (TestDBEntities context = new TestDBEntities())

                {

                    ChildTable c = new ChildTable();

                    c.ID = 4;

                    c.Name = "Child";

                    c.ParentTableReference.EntityKey = new EntityKey("TestDBEntities.ParentTable", "ID", 1);

                    context.AddToChildTable(c);

                    context.SaveChanges();

         }

    ======================================================================================

     

     

    Besides, in EF4 we have the new Foreign Key Association.   So we can keep the foreign key column in the entity class.  Directly setting these foreign key properties can solve the problem conveniently.    For detail, please see

    http://blogs.msdn.com/adonet/archive/2009/11/06/foreign-key-relationships-in-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx

    http://msdn.microsoft.com/en-us/library/ee373856%28VS.100%29.aspx

     

    If you have any questions, please feel free to let me know.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, March 20, 2010 6:32 AM
    Moderator

All replies

  • Hello Marco,

     

    Welcome to MSDN Forums!

     

    From your sample codes, I think you are using Entity Framework instead of LINQ to SQL, so I will move this thread to EF forum. 

     

    Based on my understanding, you want to directly set the foreign key column of the Reserve table, right?    It seems that the CLIENT, PRODS, WS_STATUS all exist in the database.   In EFv1 (.NET 3.5 SP1), I have three methods for your references:

     

    1.      Retrieve the entities from the database and set them to the navigation properties of the Reserve entity.  In this method, there will be several database calls to retrieve the existing entities.  So if you don’t want first retrieve these entities, please refer to the other two workarounds.

     

    2.      Create certain entities with only the entity key (Primary Key) and attach these entities to the ObjectContext.  Then we can set these entities to the navigation properties of the Reserve entity.   EF tracks the entity via their entity keys.  After we attach the entities, EF will treat them as existing entities in the database.   

    ======================================================================================

                using (TestDBEntities context = new TestDBEntities())

                {

                    ChildTable c = new ChildTable();

                    c.ID = 1;

                    c.Name = "Child";

                    ParentTable p = new ParentTable() { ID = 1 };

                    context.AttachTo("ParentTable", p);

                    c.ParentTable = p;

                    context.AddToChildTable(c);

                    context.SaveChanges();

         }

    ======================================================================================

     

    3.      Set the EntityReference.EntityKey directly.  

    ======================================================================================

                using (TestDBEntities context = new TestDBEntities())

                {

                    ChildTable c = new ChildTable();

                    c.ID = 4;

                    c.Name = "Child";

                    c.ParentTableReference.EntityKey = new EntityKey("TestDBEntities.ParentTable", "ID", 1);

                    context.AddToChildTable(c);

                    context.SaveChanges();

         }

    ======================================================================================

     

     

    Besides, in EF4 we have the new Foreign Key Association.   So we can keep the foreign key column in the entity class.  Directly setting these foreign key properties can solve the problem conveniently.    For detail, please see

    http://blogs.msdn.com/adonet/archive/2009/11/06/foreign-key-relationships-in-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

    http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx

    http://msdn.microsoft.com/en-us/library/ee373856%28VS.100%29.aspx

     

    If you have any questions, please feel free to let me know.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, March 20, 2010 6:32 AM
    Moderator
  • Hello Lingzhi,

      Thank you very much for your precise answer. I'll use the third option (less code write). About the EF4, let me ask you. I use the database FireBird and I create the EDMX file using EdmGen2. It is a very fast way for doing it, In VS it lakes a long time for the same work. My database has 270 tables and I place all them in the single EDMX file. It is working. The question is: what happens in EF4? Can I stay creating EDMX in the same way (EdmGen2), or there is another way for it?

      Thanks,

      Marco Castro

    Monday, March 22, 2010 11:00 AM
  • Hello Marco,

     

    I think VS IDE will be slower than the EdmGen and EdmGen2 tools.  But 270 tables in one EDM can make the model a little large.   Since VS2010 and EF4 is still in pre-release status, I would recommend you post this question at the EF pre-release forum for better support, http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads.   Many product team members are helping us and collecting feedbacks there. 

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, March 23, 2010 3:53 AM
    Moderator