0

I have:

Entity Framework DB first

public partial class Departments
{
    public string Name_Department { get; set; }
    public int Department_ID { get; set; }
}

When i was refreshed model all modify in file was cleared

I need:

How can i add attribute property to Serialize XML like:

[Serializable]
[XmlType(nameof(Departments))]
public partial class Departments
{
    [XmlAttribute("Name_Department")]
    public string Name_Department { get; set; }
    [XmlAttribute("Department_ID")]
    public int Department_ID { get; set; }
}

2 Answers 2

1

You have to make the generated properties private in the properties window. Then you have to create new public properties that reference the private properties and add the attributes to them. In the properties window you can also rename your now private properties so that you can name your public properties like the original ones:

Generated code:

public partial class Departments
{
    private string Name_DepartmentCore { get; set; }
    private int Department_IDCore { get; set; }
}

Custom code (in C# 7.0):

[Serializable]
[XmlType(nameof(Departments))]
public partial class Departments
{
    [XmlAttribute("Name_Department")]
    public string Name_Department {
        get => Name_DepartmentCore;
        set => Name_DepartmentCore = value;
    }
    [XmlAttribute("Department_ID")]
    public int Department_ID {
        get => Department_IDCore;
        set => Department_IDCore = value;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

A very interesting way. Thank you
Note that if you want to use those properties for queries, you still need access to the original properties.
1

Your class model is generated when you are using DB first, that is by design.

Solution 1. Microsoft is dropping support for database first, and edmx is depricated. One solution would be to use CODE FIRST instead. This would allow you to decorate your POCO classes with attributes.

Solution 2. Create a parallel model just for XML serialization.

1 Comment

This database is very old (even there is * .dbf). Unfortunately, I can not change anything there. Have to adjust to the fact that there is. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.