locked
Saving Data to Database using Entity Framework RRS feed

  • Question

  • Hi All.

    Help needed again.

    I need to save data to table and to its reference table.

    This is my entity diagram

    Main Table: Contact:

     

    Contact----(1----0..1)-----Position

    Contact----(1----0..1)-----SpokenLanguage

    Contact----(1----0..1)-----Department

     

    CommuncationContact ------> Contact that is CommuncationContact table is inheriting Contact.

     

    Here is the code:

    Domain.Contact AddContact = new Domain.Contact();  
     
    SpokenLanguage UserLanguage=new SpokenLanguage();  
    Position userPosition=new Position();  
    Department UserDepartment=new Department();  
     
    AddContact.Common_SpokenLanguage=UserLanguage;  
    AddContact.Common_Position=userPosition;  
    AddContact.Common_Department=UserDepartment;  
     
    AddContact.UserId=txtId.Text;  
     
    //Now i need to set value for of status property in Position field  
    //what should i use??  
     
    AddContact.Common_Position.Status=....  
    //Is this correct?  
     
     


    Again CommunicationContact entity is inheriting table Contact.

    How to add data to CommuncatioNContact Table also?

    Do anybody have example of storing data to a table , to its reference table and to inherited table using Entity ??

    Varun

     

     

    Friday, January 2, 2009 10:17 AM

Answers

  • What you want to do is not clear from the above code: It does not seem to deal with a CommunicationContact at all. If you want to create a CommunicationContact entity, you simply need to write:

    Domain.CommunicationContact communicationContact = new Domain.CommunicationContact();

    You can then set properties on it as appropriate - and yes, you can write AddContact.CommonPosition.Status = foo and that should work just fine.

    If an EntityType is mapped to two tables (e.g. Contacts and CommunicationsContacts), simply creating an instance of that EntityType, adding it to the context, and calling SaveChanges will result in the Entity Framework writing the appropriate data to both tables.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, January 2, 2009 6:12 PM