none
Inserting data to tables with 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:53 AM

Answers

  • Hi xandeep;

    I am assuming that the Key columns in the database is set to Identity and therefore auto-generated by the database. That being the case then your code below will work and is the correct way of adding a student into the Student table with its related table entries into TemporaryAddress and PermanentAddress. Entity Framework will take care of updating the keys of all the tables.

    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";
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by xandeep Saturday, December 24, 2011 4:28 PM
    Friday, December 23, 2011 3:39 PM
  • Hi xandeep;

    I think you marked the wrong solution as the answer to the question. You stated this in your first post. 

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

     Then in your second post you stated this.

    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.

     With my solution I answered your question. So in answering your question do I not get credit for it?

    To your last question. 

    Is it a good idea to get started with entity framework directly?

     The following is just my opinion. I believe that you would be better off starting with ADO.Net building the connection string adapters using data readers so that you know how to work with them. Many companies have not yet moved onto Entity Framework and so you may take a position with a company which is still doing it with ADO.Net. Then once you have an understanding of how it works then move on to Entity Framework which by the way uses ADO.Net to communicate with the database and this way you can appreciate what is going on underneath the covers. Entity Framework is a powerful technology but is not a panacea. For example Entity Framework has no bulk update function so if you have lets say 100 records in the ObjectContext and you save them to the database you will be hitting the database 100 time once for each record that needs to be updated and which will flood the network with calls.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by xandeep Saturday, December 24, 2011 4:31 PM
    Saturday, December 24, 2011 2:54 PM
  • On 12/24/2011 2:37 AM, xandeep wrote:
    > thanks for your quick reply fenando.. :)
    >
    > As i told i am just a beginner - a final year Computer Engineering
    > student, and I am learning ASP.net with C#, in future also i am sure i
    > will be having a lots of problem.
    >
    > i want to ask you one more question, I used to do programing with PHP
    > and mysql.
    >
    >
    > now migrating to .net framework, and with very sound fundamentals of
    > programming i have,
    >
    >
    > *Is it a good idea to get started with entity framework directly??*
     
    No -- You should learn ADO.NET and T-SQL. And also learn how to use SQL
    Server. After you learn the basics on that, then you can learn EF. You
    have to know that stuff I talk about first, which is the foundation upon
    which EF has been built on.
     
    If things don't work right with EF, then what are you going to do if you
    don't know the underlying basics? Also, EF is not a stops all and ends
    all solution, and something you may try to do EF can't support it. Then
    you may have to fallback to straight-up ADO.NET and T-SQL.
    >
    > I havnt worked with ASP.net, just learned basics abt how to do things
    > and now i am deep diving to EF and willing to master Entity Framework,
    >
    > After this i Want to learn MVC style of coding.... MS-SQL BI and
    > SharePoint as well :)
    >
    > please tell me if its a good idea?? to directly start working with EF.
     You need to forget about the MVC, ASP.NET and EF for right now. You need
    to understand Object Oriented Programming that everything you talk about
    is based upon.
     
     
    <copied>
     
    Design patterns are recurring solutions to software design problems you
    find again and again in real-world application development. Patterns are
    about design and interaction of objects, as well as providing a
    communication platform concerning elegant, reusable solutions to
    commonly encountered programming challenges.
     
    <end>
     
    You may want to check out the Dofactory teaching materiel the link on
    the right side of the page.
     
    • Marked as answer by xandeep Saturday, December 24, 2011 4:33 PM
    Saturday, December 24, 2011 3:12 PM
  • On 12/24/2011 11:44 AM, xandeep wrote:
    > thank you Darnold :)
    >
    > Actually i know little bit..about using SQL server and i have sound
    > database fundamentals.
    >
    > but I am having difficulty in getting started with ADO.net,
    > thanks for the links u shared, I really appreciate it.. I am going
    > through it...
    > pls share some more links u know might be useful for me.
    > I also have a plural sight subscriptions, any course in pluralsight
    > suitable for me? to learn ADO.net, please share it as well :)
    >
     
    I would say that you go back to before EF hit the scene. You might be
    able to find an e-book.
     
     
    I saw you talking about MVC. But there is also MVP that can be applied
    to ASP.NET form and also Windows form applications as well.
     
     
    Find yourself a good book on this
     
     
    • Marked as answer by xandeep Sunday, December 25, 2011 10:18 AM
    Saturday, December 24, 2011 11:18 PM
  • 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 xandeep Saturday, December 24, 2011 7:30 AM
    Friday, December 23, 2011 12:01 PM
  •  

    Not a problem I thought that is what happened. As far as resources on the net it been a while since I looked or seen any links to this subject although if you do a search on ADO.Net Data Access you should get a ton of stuff.

    You might be able to find some books in your local library on ADO.Net

    Good luck

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by xandeep Saturday, December 24, 2011 5:40 PM
    Saturday, December 24, 2011 5:37 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 xandeep Saturday, December 24, 2011 7:30 AM
    Friday, December 23, 2011 12:01 PM
  • Hi xandeep;

    I am assuming that the Key columns in the database is set to Identity and therefore auto-generated by the database. That being the case then your code below will work and is the correct way of adding a student into the Student table with its related table entries into TemporaryAddress and PermanentAddress. Entity Framework will take care of updating the keys of all the tables.

    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";
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by xandeep Saturday, December 24, 2011 4:28 PM
    Friday, December 23, 2011 3:39 PM
  • thanks for your quick reply fenando.. :)

    As i told i am just a beginner - a final year Computer Engineering student,  and I am learning ASP.net with C#, in future also i am sure i will be having a lots of problem.

    i want to ask you one more question, I used to do programing with PHP and mysql.


    now migrating to .net framework, and with very sound fundamentals of programming i have,


    Is it a good idea to get started with entity framework directly??

    I havnt worked with ASP.net, just learned basics abt how to do things and now i am deep diving to EF and willing to master Entity Framework,

    After this i Want to learn MVC style of coding.... MS-SQL  BI and SharePoint as well :)

    please tell me if its a good idea?? to directly start working with EF.


    Sandeep
    • Edited by xandeep Saturday, December 24, 2011 9:41 AM
    Saturday, December 24, 2011 7:37 AM
  • Hi xandeep;

    I think you marked the wrong solution as the answer to the question. You stated this in your first post. 

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

     Then in your second post you stated this.

    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.

     With my solution I answered your question. So in answering your question do I not get credit for it?

    To your last question. 

    Is it a good idea to get started with entity framework directly?

     The following is just my opinion. I believe that you would be better off starting with ADO.Net building the connection string adapters using data readers so that you know how to work with them. Many companies have not yet moved onto Entity Framework and so you may take a position with a company which is still doing it with ADO.Net. Then once you have an understanding of how it works then move on to Entity Framework which by the way uses ADO.Net to communicate with the database and this way you can appreciate what is going on underneath the covers. Entity Framework is a powerful technology but is not a panacea. For example Entity Framework has no bulk update function so if you have lets say 100 records in the ObjectContext and you save them to the database you will be hitting the database 100 time once for each record that needs to be updated and which will flood the network with calls.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by xandeep Saturday, December 24, 2011 4:31 PM
    Saturday, December 24, 2011 2:54 PM
  • On 12/24/2011 2:37 AM, xandeep wrote:
    > thanks for your quick reply fenando.. :)
    >
    > As i told i am just a beginner - a final year Computer Engineering
    > student, and I am learning ASP.net with C#, in future also i am sure i
    > will be having a lots of problem.
    >
    > i want to ask you one more question, I used to do programing with PHP
    > and mysql.
    >
    >
    > now migrating to .net framework, and with very sound fundamentals of
    > programming i have,
    >
    >
    > *Is it a good idea to get started with entity framework directly??*
     
    No -- You should learn ADO.NET and T-SQL. And also learn how to use SQL
    Server. After you learn the basics on that, then you can learn EF. You
    have to know that stuff I talk about first, which is the foundation upon
    which EF has been built on.
     
    If things don't work right with EF, then what are you going to do if you
    don't know the underlying basics? Also, EF is not a stops all and ends
    all solution, and something you may try to do EF can't support it. Then
    you may have to fallback to straight-up ADO.NET and T-SQL.
    >
    > I havnt worked with ASP.net, just learned basics abt how to do things
    > and now i am deep diving to EF and willing to master Entity Framework,
    >
    > After this i Want to learn MVC style of coding.... MS-SQL BI and
    > SharePoint as well :)
    >
    > please tell me if its a good idea?? to directly start working with EF.
     You need to forget about the MVC, ASP.NET and EF for right now. You need
    to understand Object Oriented Programming that everything you talk about
    is based upon.
     
     
    <copied>
     
    Design patterns are recurring solutions to software design problems you
    find again and again in real-world application development. Patterns are
    about design and interaction of objects, as well as providing a
    communication platform concerning elegant, reusable solutions to
    commonly encountered programming challenges.
     
    <end>
     
    You may want to check out the Dofactory teaching materiel the link on
    the right side of the page.
     
    • Marked as answer by xandeep Saturday, December 24, 2011 4:33 PM
    Saturday, December 24, 2011 3:12 PM
  • thank you Fernando, thank you once again for your quick reply....

    and ya , i though i marked your answer as answer but I mistook and marked mine as answer..sorry for that.. :)

    I will start learning as you told.... please share if u have some good resource full website or useful important links on msdn.

    I also have a plural sight subscriptions, any course in pluralsight suitable for me? to learn ADO.net, please share it as well

    you know i am not a beginner with database concepts so, I am not sure where to start from in ADO.NET.



    Sandeep
    • Edited by xandeep Saturday, December 24, 2011 4:49 PM
    Saturday, December 24, 2011 4:41 PM
  • thank you Darnold :)

    Actually i know little bit..about using SQL server and i have sound database fundamentals.

    but I am having difficulty in getting started with ADO.net,
    thanks for the links u shared, I really appreciate it.. I am going through it...
    pls share some more  links u know might be useful for me.
    I also have a plural sight subscriptions, any course in pluralsight suitable for me? to learn ADO.net, please share it as well :)

     


    Sandeep

    • Edited by xandeep Saturday, December 24, 2011 4:48 PM
    Saturday, December 24, 2011 4:44 PM
  •  

    Not a problem I thought that is what happened. As far as resources on the net it been a while since I looked or seen any links to this subject although if you do a search on ADO.Net Data Access you should get a ton of stuff.

    You might be able to find some books in your local library on ADO.Net

    Good luck

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by xandeep Saturday, December 24, 2011 5:40 PM
    Saturday, December 24, 2011 5:37 PM
  • On 12/24/2011 11:44 AM, xandeep wrote:
    > thank you Darnold :)
    >
    > Actually i know little bit..about using SQL server and i have sound
    > database fundamentals.
    >
    > but I am having difficulty in getting started with ADO.net,
    > thanks for the links u shared, I really appreciate it.. I am going
    > through it...
    > pls share some more links u know might be useful for me.
    > I also have a plural sight subscriptions, any course in pluralsight
    > suitable for me? to learn ADO.net, please share it as well :)
    >
     
    I would say that you go back to before EF hit the scene. You might be
    able to find an e-book.
     
     
    I saw you talking about MVC. But there is also MVP that can be applied
    to ASP.NET form and also Windows form applications as well.
     
     
    Find yourself a good book on this
     
     
    • Marked as answer by xandeep Sunday, December 25, 2011 10:18 AM
    Saturday, December 24, 2011 11:18 PM