Search code examples
asp.net-mvcentity-frameworkentity-framework-6

Filling tables of classes that are used in viewmodel


I have a viewmodel that includes two classes. These classes are in relation with another classes , one-to-many and one-to-one relations. How can I pass data from viewmodel into tables that thay have relation with another tables? for example:

public class A
{
    public int a_id { get; set; }
    public string a_field1 { get; set; }
    ...
    //one-to-many with B
    public virtual ICollection<B> Bs { get; set; }
    //one-to-one with D
    public virtual D Ds { get; set; }
}
public class B
{
    public int B_id { get; set; }
    public string B_field1 { get; set; }
    ...
    public int a_id { get; set; }
    public int c_id { get; set; }
    // one-to-many with A , C
    public virtual A As { get; set; }
    public virtual C Cs { get; set; }
}
public class C
{
    public int C_id { get; set; }
    public string C_field1 { get; set; }
    ...
    // one-to-many with B
    public virtual ICollection<B> Bs { get; set; }
    // one-to-one with E
    public virtual E Es { get; set; }
}

My viewmodel is maked from A and B. How to add infos ,into A that has a collection from B and virtual instance from D? Or in B that has virtual instance from A and C? Do I change my viewmodel? Please guide and advise me.


Solution

  • i find my answer: when you want to make view model from classes like above ,you don't need use virtual collection or virtual property in view model. you should use just properties that you need. in this example ,your view model from A and B this will be:

    public class VM_A_B
    {
    
        public int A_id { get; set; }
        public int A_Field1 { get; set; }
        public int b_id { get; set; }
        public int b_Field1 { get; set; }
        //no need any virtual collection or virtual property
    }
    

    Good Luck.