1

I'm working on an MVC3 application. I created my POCO classes through the ADO.NET DbContext Generator and I'm using partial classes to add validation on properties. Now, when I try to serialize one of my entities I receive this error:

"Cannot serialize member .... of type 'System.Collections.Generic.ICollection`1[....."

I googled this error and I discovered that it's possible to add the tag [XmlIgnore] to certain properties. But the point is that I can't put this tag on the properties because they are created everytime by the generator. So how I can do this in a simpler way ?

Alex R.
  • 4,664
  • 4
  • 30
  • 40
Daniela
  • 11
  • 1

1 Answers1

1

The key is the MetadataTypeAttribute. You can add this to your partial class which implements the additional properties and your validation logic. Then create a meta data class with a property of the same name of your generated class, and apply the attribute you need.

[MetadataType(typeof(MyPOCOMetaData))]
public partial class MyPOCO
{
    // your partial validation code and properties
}

public class MyPOCOMetaData
{
    [XmlIgnore]
    public string GenerateProperyName { get; set; }
}
DanielB
  • 19,910
  • 2
  • 44
  • 50
  • I do this but it doesn't work [MetadataType(typeof(Cliente_Validation))] public partial class Cliente { } public class Cliente_Validation { [XmlIgnore] public virtual ICollection Iscrizioni { get; set; } [XmlIgnore] public virtual ICollection Vendite { get; set; } } – Daniela Jul 15 '11 at 10:08
  • Does it work if you remove `virtual`from the meta data class property? I can't imagine that XmlSerializer ignores the meta data type. – DanielB Jul 15 '11 at 10:46
  • i tried this too, but it doesn't work...[MetadataType(typeof(Cliente_Validation))] public partial class Cliente { } public class Cliente_Validation { [XmlIgnore] public virtual ICollection Iscrizioni { get; set; } [XmlIgnore] public virtual ICollection Vendite { get; set; } } – Daniela Jul 15 '11 at 11:51