none
Inserting data to tableswith one to one relation RRS feed

  • Question

  • I am a beginner and learning EF,
    Hers is my model (MODEL FIRST)

    now i Want to add students with there country and city ( just a sample for Permanent and temporary address )

    Each student will have only one permanent and Temporary address , so Primary key for student table will be the primary key for both the address table as well.

    I dd many things but Failed Every time so finally decided to ask here.
    I wrote as follows as well, but its not working,




     protected void add_Click(object sender, EventArgs e)
        {
    
          Model.PermanentAddress Paddress = new Model.PermanentAddress()
          {
              Id=5,
             Country=country.Text               
          };
    
          Model.TemporaryAddress Taddress = new Model.TemporaryAddress()
          {
              Id = 5,
              CityDistrict = cityDistrict.Text
          };
       
         Model.Student NewStudent= new Model.Student()
         {
            Id=5,
            Fname= fname.Text,
            Mname=mname.Text,
            Lname= lname.Text,
           
    
          
       
          };
         
    
    using (Model.ModelContainer ctx = new Model.ModelContainer())
            {
                ctx.PermanentAddresses.AddObject(Paddress);
                ctx.TemporaryAddresses.AddObject(Taddress);
                ctx.Students.AddObject(NewStudent);
                ctx.SaveChanges();
                result.Text = "new Student Saved";
    
    
            }
        }
    
    
    The Error goes like this, please help me in what this s saying to me ,

    Friday, December 23, 2011 9:52 AM

Answers

  • I so

    lved this problem like this and it works fine..
    But i am not confident it this is a good way, so pleas  contribute your  thinking.. :) and solutions..

     

     

     Model.Student NewStudent= new Model.Student()
         {
            
            Fname= fname.Text,
            Mname=mname.Text,
            Lname= lname.Text,
    
            
    
           
        
          };
         NewStudent.TemporaryAddress = new TemporaryAddress { CityDistrict = cityDistrict.Text };
    
         NewStudent.PermanentAddress = new PermanentAddress { Country = country.Text };
          
    
    using (Model.ModelContainer ctx = new Model.ModelContainer())
            {
               
                ctx.Students.AddObject(NewStudent);
                ctx.SaveChanges();
                result.Text = "new Student Saved";
    
    
            }
        
    


     


    Sandeep
    • Edited by xandeep Friday, December 23, 2011 12:01 PM
    • Marked as answer by Alan_chenModerator Monday, December 26, 2011 7:04 AM
    Friday, December 23, 2011 12:00 PM

All replies

  • I so

    lved this problem like this and it works fine..
    But i am not confident it this is a good way, so pleas  contribute your  thinking.. :) and solutions..

     

     

     Model.Student NewStudent= new Model.Student()
         {
            
            Fname= fname.Text,
            Mname=mname.Text,
            Lname= lname.Text,
    
            
    
           
        
          };
         NewStudent.TemporaryAddress = new TemporaryAddress { CityDistrict = cityDistrict.Text };
    
         NewStudent.PermanentAddress = new PermanentAddress { Country = country.Text };
          
    
    using (Model.ModelContainer ctx = new Model.ModelContainer())
            {
               
                ctx.Students.AddObject(NewStudent);
                ctx.SaveChanges();
                result.Text = "new Student Saved";
    
    
            }
        
    


     


    Sandeep
    • Edited by xandeep Friday, December 23, 2011 12:01 PM
    • Marked as answer by Alan_chenModerator Monday, December 26, 2011 7:04 AM
    Friday, December 23, 2011 12:00 PM
  • Hi xandeep,

    I think you're in a right way now, thanks for your post.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 26, 2011 7:04 AM
    Moderator