【问题标题】:Creating a Entity Framework Buddy Class in a VB.NET MVC Application to extend validation在 VB.NET MVC 应用程序中创建实体框架伙伴类以扩展验证
【发布时间】:2012-11-21 10:11:37
【问题描述】:

在实体数据模型中,我有以下复杂类型

<EdmComplexTypeAttribute(NamespaceName:="MEMORABLEModel", Name:="EDMspGetContact_Result")>
<DataContractAttribute(IsReference:=True)>
<Serializable()>
Partial Public Class EDMspGetContact_Result
    Inherits ComplexObject
#Region "Factory Method"
.... etc

我正在 VB.Net 中编写一个 MVC 4.0 应用程序

我想在我的解决方案中为另一个项目中的复杂类型定义添加验证

我试图在我的 MVC 项目中定义一个部分类,但即使它编译我知道它在某种程度上是错误的 因为脚手架模板不会生成所有属性

在过去的 6 个月里,我已经从传统编码转向面向对象编码,我真的很难克服这样的问题。感谢指导我哪里出错了。 我试过添加一个命名空间。

<MetadataType(GetType(EDMspGetContact_ResultMD))> _
<ScaffoldTable(True)> _
Partial Public Class EDMspGetContact_Result
    ' Note this class has nothing in it.  It's just here to add the class-level attribute.
End Class

Public Class EDMspGetContact_ResultMD
    '    ' Name the field the same as EF named the property - "FirstName" for example.
    '    ' Also, the type needs to match.  Basically just redeclare it.
    '    ' Note that this is a field.  I think it can be a property too, but fields definitely should work.

    <Required()> _
    <Display(name:="Last Name")> _
    Public LastName As String

End Class

【问题讨论】:

    标签: asp.net-mvc vb.net entity-framework-4 buddy-class


    【解决方案1】:

    以上哪个类定义是EF生成的模型类?如果是最后一个,也需要将其标记为 Partial,以便您的分部类中定义的扩展成员成为同一类的一部分。这可能意味着更改 EF 用于生成模型类的 T4 模板。

    这是假设您首先使用数据库或模型。如果您首先使用代码,我想您只需要将主模型类设为局部即可。

    【讨论】:

    • EDMspGetContact_Result 类是 EF 生成的类。我正在使用数据库生成的存储过程来获取复杂类型。它们都被声明为部分但 MVC 应用程序没有选择 EDM 类?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多