【问题标题】:Filling tables of classes that are used in viewmodel填充视图模型中使用的类表
【发布时间】:2016-06-13 14:33:16
【问题描述】:

我有一个包含两个类的视图模型。 这些类与另一个类相关,一对多和一对一的关系。 如何将数据从视图模型传递到与其他表有关系的表中? 例如:

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

我的视图模型是由 A 和 B 组成的。 如何将信息添加到具有来自 B 的集合和来自 D 的虚拟实例的 A 中? 或者在 B 中具有来自 A 和 C 的虚拟实例? 我要更改我的视图模型吗? 请指导和建议。

【问题讨论】:

    标签: asp.net-mvc entity-framework entity-framework-6


    【解决方案1】:

    我找到了答案: 当你想从上面的类中创建视图模型时,你不需要在视图模型中使用虚拟集合或虚拟属性。 你应该只使用你需要的属性。 在此示例中,您的 A 和 B 视图模型将是:

    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
    }
    

    祝你好运。

    【讨论】:

    • 这是一个相当模糊的答案......如果您包含一些代码,它将对未来的读者更有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多