如何将xml属性添加到实体生成的分部类中

时间:2015-04-30 09:33:20

标签: xml vb.net entity-framework

我需要从xml文件导入用户。我的用户类由Entity Framewok 5自动生成,类的propreties名称与xml属性的名称不同。我不能改变它。

XML结构:

<?xml version="1.0" encoding="UTF-8"?>
<LISTE_EMPLOYES>
  <EMPLOYE>
    <user_unique_key>A0001</user_unique_key>
    <user_job_matricule>001017</user_job_matricule>
    <user_etaciv_nom>DOE</user_etaciv_nom>
    <user_etaciv_prenom>JOHN</user_etaciv_prenom>
    <user_etaciv_genre>M</user_etaciv_genre>
</EMPLOYE>
<EMPLOYE>
    <user_unique_key>A0003</user_unique_key>
    <user_job_matricule>005017</user_job_matricule>
    <user_etaciv_nom>DOE</user_etaciv_nom>
    <user_etaciv_prenom>PAT</user_etaciv_prenom>
    <user_etaciv_genre>F</user_etaciv_genre>
</EMPLOYE>
</LISTE_EMPLOYES>

部分类自动生成

Partial Public Class User
    Public Property unique_key As String
    Public Property matricule As String
    Public Property nom As String
    Public Property prenom As String
    Public Property genre As String
End Class

那么可以添加具有类似数据注释机制的xml属性吗?

    <MetadataType(GetType(UserMD))> _ Partial Public Class User End Class

Public Class UserMD
    <StringLength(60)> _ Public Property nom As Object

    <StringLength(60)> _ Public Property prenom As Object
End class

感谢

2 个答案:

答案 0 :(得分:0)

感谢您的帮助。我使用基于你的“另一种”解决方案:

    <XmlRoot("LISTE_EMPLOYES")>
    Public Class Liste_Employees
        Private m_employee As List(Of Employee)
        <XmlElement("EMPLOYE")>
        Public Property employee As List(Of Employee)
            Get
                Return m_employee
            End Get
            Set(value As List(Of Employee))
                m_employee = value
            End Set
        End Property
    End Class

    <XmlRoot("EMPLOYE")>
    Public Class Employee
        Private _u as User

    Public Property user_unique_key As String
        Get
            Return _u.unique_key
        End Get
        Set(value As String)
            _u.unique_key = value
        End Set
    End Property

    Public Property user_job_matricule As String
        Get
            Return _u.matricule
        End Get
        Set(value As String)
            _u.matricule = value
        End Set
    End Property ...
End Class

答案 1 :(得分:0)

抱歉,买我的答案是在c#,但应该很简单,以适应vb。 EF会在您的示例中为您创建一个类

Partial Public Class User
Public Property unique_key As String
Public Property matricule As String
Public Property nom As String
Public Property prenom As String
Public Property genre As String
End Class

你可以做你想做的一种方法是创建一个单独的文件,即Partials.cs。在此单独的文件中,您可以保留部分类的附加信息。对于上面的课程(在c#中),你会做类似

的事情
[MetadataType(typeof(UserMetaData))]
[XmlElement("User")]
public partial class User{}

public partial class UserMetaData
{
[XmlElement("user_unique_key")]
public string unique_key;
[XmlElement("user_job_matricule")]
public string matricule;
[XmlElement("user_etaciv_nom")]
public string nom;
[XmlElement("user_etaciv_prenom")]
public string prenom;
[XmlElement("user_etaciv_genre")]
public string genre;
}

这基本上告诉您的部分类从UserMetaData类获取元数据,您可以根据需要为每个属性分配尽可能多的属性。唯一需要注意的是,如果您向表中添加更多列并使用EF重新导入,则需要在部分类中添加这些列,如果您想控制名称。

再次抱歉c#,但我不使用vb​​,但应该很容易转换。

希望它能帮助您实现目标。

干杯 安迪