locked
how to insert data to multiple tables having relationships RRS feed

  • Question

  • Hi ,

    I'm beginner in EF , I want to know how i can add in multiple tables having relationships consisting of FK.

    suppose there is person , vehicle , person details tables.

    I want to insert record to person details table than will add the respected id as an fk in person table with some record , and than add that respected person table new row id to sale table with some infomation.

    Thanks


    • Edited by Shan Khan Tuesday, August 28, 2012 6:39 PM
    Tuesday, August 28, 2012 6:08 PM

Answers

  • Hi Shan khan,

    Please modify the bold code as below.

    Vehicle car = (from a in gm.Vehicles where a.model == carname select a).SingleOrDefault();

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Shan Khan Wednesday, August 29, 2012 9:44 AM
    Wednesday, August 29, 2012 9:32 AM

All replies

  • Hi Shan khan,

    Welcome to MSDN Forum.

    Below is a sample, the relationship between Person and PersonDetail is one to one, the relationship between Person and Vehicle is one to many.

    static void Main(string[] args)
            {
                using (TestContext context = new TestContext())
                {
                    //Instantiate a new Person
                    Person p = new Person();
                    p.Name = "Allen";
    
                    //Instantiate a new PersonDetail
                    PersonDetail detail = new PersonDetail();
                    detail.Married = false;
    
                    //Instantiate two Vehicle
                    Vehicle v1 = new Vehicle();
                    v1.TotalWheels = 2;
                    Vehicle v2 = new Vehicle();
                    v2.TotalWheels = 4;
    
                    //Query out an existing vehicle from database
                    Vehicle v3 = context.vehicleSet.Where(x => x.TotalWheels == 8).SingleOrDefault();
    
                    //Relate the instances
                    p.Detail = detail;
                    p.Vehicle.Add(v1);
                    p.Vehicle.Add(v2);
                    p.Vehicle.Add(v3);
    
                    context.personSet.Add(p);
    
                    //After SaveChanges, the four records will persist into three tables in the database
                    context.SaveChanges();
                   
                }

    Best Regards 

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Cryo75 Wednesday, August 29, 2012 6:16 AM
    Wednesday, August 29, 2012 3:02 AM
  • Hi Shan khan,

    Welcome to MSDN Forum.

    Below is a sample, the relationship between Person and PersonDetail is one to one, the relationship between Person and Vehicle is one to many.

    static void Main(string[] args)
            {
                using (TestContext context = new TestContext())
                {
                    //Instantiate a new Person
                    Person p = new Person();
                    p.Name = "Allen";
    
                    //Instantiate a new PersonDetail
                    PersonDetail detail = new PersonDetail();
                    detail.Married = false;
    
                    //Instantiate two Vehicle
                    Vehicle v1 = new Vehicle();
                    v1.TotalWheels = 2;
                    Vehicle v2 = new Vehicle();
                    v2.TotalWheels = 4;
    
                    //Query out an existing vehicle from database
                    Vehicle v3 = context.vehicleSet.Where(x => x.TotalWheels == 8).SingleOrDefault();
    
                    //Relate the instances
                    p.Detail = detail;
                    p.Vehicle.Add(v1);
                    p.Vehicle.Add(v2);
                    p.Vehicle.Add(v3);
    
                    context.personSet.Add(p);
    
                    //After SaveChanges, the four records will persist into three tables in the database
                    context.SaveChanges();
                   
                }

    Best Regards 

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Yes the relationship i made btw vehicle and sale is one to many , how i can add data in one to many relationship tables.

    Thanks

    Wednesday, August 29, 2012 7:20 AM
  • Hi Shan khan,

    The code I posted has already contains this scenario. The relationship between Person and Vehicle is one to many, so in Person entity, there's Vehicle collection, just add the Vehicle into the collection is ok. Please look at the section of 'p.Vehicle.Add(v1)...'.

    Best Regards 


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 29, 2012 9:18 AM
  • Thank you ,

    Im getting cast error.

    Following is my code :

                                                            

    im trying to cast object "obj" to vehicle "car" but im getting this error :Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[GloryMotors.EF.Vehicle]' to type 'GloryMotors.EF.Vehicle'.

     try
                {
                             per.PersonDetails.Add(pDetails);
                            sl.Person = per;
                            gm.AddToPeople(per);
                            string[] varray = lstCars.SelectedItem.Text.ToString().Split('-');
                            string carname = varray[1];
    var obj = from a in gm.Vehicles 
                                          where a.model == carname       
                                          select a;
                            Vehicle car = (Vehicle)obj;
                            sl.Vehicle = car;
                            gm.AddTosales(sl);
                            gm.SaveChanges();
                            lblSummary.Text = "Added Successfully.";
                }
                catch (Exception ex)
                {
                    lblSummary.Text = "Unable To Add. " + ex.Message;


                }

    Wednesday, August 29, 2012 9:21 AM
  • Hi Shan khan,

    Please modify the bold code as below.

    Vehicle car = (from a in gm.Vehicles where a.model == carname select a).SingleOrDefault();

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Shan Khan Wednesday, August 29, 2012 9:44 AM
    Wednesday, August 29, 2012 9:32 AM