【问题标题】:Specify inherited property type of as a specific child将继承的属性类型指定为特定子项
【发布时间】:2013-08-19 10:26:59
【问题描述】:

所以,我的实体模型中有一个继承层次结构,如下所示:

class Member
{
    public virtual Information Information { get; set; }
    public long InformationId { get; set; }

    //Other Props
}
class Staff : Member
{
}
class Guest : Member
{
}

class Information
{
    public string Name { get; set; }
}

class StaffInformation : Information
{
    public DateTime BirthDate { get; set; }
}

class GuestInformation : Information
{
    public DateTime Expiry { get; set; }
}

鉴于,我很难将会员信息转换为正确的孩子。例如,我想:

TextBoxFor(model => model.Staff.StaffInformation.BirthDate)

但我能做的是:

TextBoxFor(model => (Entities.StaffInformation)(model.Staff.Information).BirthDate)

我可以指定子项的信息类型吗?一些类似以下伪的东西:

class Staff : Member
{
    public StaffInformation Information { get; set; }
}
class Guest : Member
{
    public GuestInformation Information { get; set; }
}

有什么建议吗?

【问题讨论】:

    标签: c# asp.net-mvc-4 inheritance entity-framework-4.1 code-first


    【解决方案1】:

    这是 ViewModel 的主要候选者。

    您的实体模型和视图模型不必是一对一的。这是控制器(服务类、控制器等)要做的工作。从一种转换到另一种。

    在一个视图上上推 BirthDate 似乎很愚蠢.. 然后在另一个视图上上推到期日期。这正是 ViewModel 的用途。

    【讨论】:

    • 毫无疑问,我同意。我正在实体中寻找另一种解决方案。如果我找到它,就不需要将实体转换为 vm,反之亦然,对吧?
    • 对.. 但是.. 稍后再考虑扩展。如果您突然需要从视图中验证模型怎么办?现在您已将DataAnnotations 直接绑定到您的模型。他们甚至是 POCO 吗?或者它们是生成的?你应该提前考虑......否则它会回来咬你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多