SerializationException

Topics: Issues / bugs, Questions
May 19, 2014 at 9:45 AM
Hello updated from catel 3.9. to the newest beta.

Now I get a SerializationException in the constructor of a viewmodel if the injected model is assigned to the [Model] property.

The Model has a reference to a Entity loaded wit EF:

'System.Data.Entity.DynamicProxies.Unit_98065721FA9632897D51A49DF2ECA45A4B87A97889FE9CC5B70412FCA18871DF'' is not known.

Use a DataContractResolver or add KnownTypeAttribute.

I already added a KnownType to the class. A class which was inheritaed from the modelclass.

I asked already
https://catel.codeplex.com/discussions/470551

But now I don't know how to add a KnownTypeAttribute to a dynamic class name (EF Entity created with proxy)

What are the changes in Serialization from catel 3.9.0 to catel 3.9.0 beta?
May 23, 2014 at 10:53 AM
No answer?
Coordinator
May 23, 2014 at 8:56 PM
The changes can be found on github in the change sets. I made so many changes that I can't recall all of them. If you can provide a reproducable case and a pull request, we are happy to accept it.
May 27, 2014 at 8:37 AM
The problem is the proxy creation in entityframework

One solution is to disable proxy creation. But then also lazy loading is disabled.

An other solution maybe is following:

http://harald-muehlhoff.de/blog/custom-datacontractresolver-to-get-rid-of-knowntype-and-ef-proxy-madness.

What do you think.

Do you have never problems with entityframework and serilization in catel?
Coordinator
May 27, 2014 at 8:54 AM
I don't use code-first (thus Catel models + EF).
May 27, 2014 at 9:00 AM
I also don't use code first. I use model first.

Sorry what do you mean with (thus Catel models + EF).

Dou you map the entitys from EF to catel models?
Coordinator
May 27, 2014 at 9:01 AM
When I use EF on projects, I create a DB and the generate the entities based on the DB. I am not sure what you are trying to achieve
May 27, 2014 at 9:43 AM
My problem is the SerializationException. If you try to serialize a entity from EF you have problems if proxy creation is enabled.
You also find here stuff in the internet, about this problem. Like the link I posted.
Coordinator
May 27, 2014 at 9:46 AM
But do you need the EF entity serialized? You can exclude it from serialization with serialization modifiers. Why are you even trying to serialize the EF entity and what has Catel to do with it?
May 27, 2014 at 10:11 AM
I don't try to serialize my entity. Catel try to serialize the entity.

Code is easier then words:
public class LocationInstanceViewModel : ViewModelBase
{
    public LocationInstanceViewModel(LocationInstance locationInstance)
    {
         LocationInstance = locationInstance;
     }

    [Model]
    public LocationInstance LocationInstance
    {
          get { return GetValue<LocationInstance>(LocationInstanceProperty); }
          private set { SetValue(LocationInstanceProperty, value); }  // the exception occurs here.
    }
    
    public static readonly PropertyData LocationInstanceProperty = RegisterProperty("LocationInstance", typeof(LocationInstance));
}


public class LocationInstance: ModelBase
{
    ....
        public ObservableCollection<Unit> Units
        {
            get { return GetValue<ObservableCollection<Unit>>(UnitsProperty); }
            set { SetValue(UnitsProperty, value); }
        }
       public static readonly PropertyData UnitsProperty = RegisterProperty("Units", typeof(ObservableCollection<Unit>));
       // Unit is the entity of EF
}
Coordinator
May 27, 2014 at 2:50 PM
You have a few options here:

1) Use [Model(SupportIEditableObject = false)]
2) Use [ExcludeFromSerialization] on Units

Note that both methods will result in the Units that will not be reverted on a cancel action.
May 28, 2014 at 8:46 AM
Thanks for your help.
Thats ok in this case I don't need the cancel action.

But: For me its still not clear why sometimes the entitys can nont be serialized. Thats not the problem of catel. But sometimes it occurcs in the serialzation of catel.

Maybe on day I or someone other find a solution :-)