22

In [a previous posting], I was set on the path to having to clone my entities. This I've attempted to do with a serialisation approach as found in [codeproject].

because the classes are generated by Entity Framework, I mark them up separately in a custom .cs like this:

[Serializable]
public partial class Claims
{
}

however, when the check (in the clone method):

if (Object.ReferenceEquals(source, null))
{

gets hit, I get the error:

System.ArgumentException was unhandled by user code
  Message=The type must be serializable.
Parameter name: source
  Source=Web
  ParamName=source
  StackTrace:
       at .Web.Cloner.Clone[T](T source) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Extensions.Object.cs:line 49
       at .Web.Models.Employer..ctor(User u) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Models\EF.Custom.cs:line 121
       at .Web.Controllers.AuthController.Register(String Company, String GivenName, String Surname, String Title, String Department) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Controllers\AuthController.cs:line 119
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

so apparently whilst my class Claims is serialisable, the dynamic proxies generated by EF are not... somehow my decorations are not flowing through.

what's the trick here?

* Update I *

for more context: I have a class User which contains a property Claims defined as an ICollection<Claim>. when doing the cloning, the type that gets passed is the collection, not Claim - this explains why the cloner is complaining that the type is not serializable. so the question now is: how do I make User.Claims serializable since I can't decorate a property?

Error   1   Attribute 'Serializable' is not valid on this declaration type.
It is only valid on 'class, struct, enum, delegate' declarations.   
C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Models\EF.Custom.cs
128 10  Website

* Update II *

the point of the exercise is to facility a deep copy. this is what it looks like:

public partial class Employer
{
    public Employer(User u)
    {
        this.Id = u.Id;
        this.GivenName = u.GivenName;
        this.Surname = u.Surname;
        this.Claims = u.Claims.Clone();
        this.Contacts = u.Contacts.Clone();
    }
}

in order for the u.Claims.Clone() to work, u.Claims must be serializable but it's not for the reasons cited above.

* Update III *

ok, I changed approach, implementing the constructor like this:

public partial class Employer
{
    public Employer(User u)
    {
        this.Id = u.Id;
        this.GivenName = u.GivenName;
        this.Surname = u.Surname;

        ICollection<Claim> cs = new List<Claim>();
        foreach (Claim c in u.Claims)
        {
            cs.Add(c.Clone());
        }
        this.Claims = cs;

and now it gets past the clone()'s check ("if" line above), but now it breaks at:

formatter.Serialize(stream, source);

with:

System.Runtime.Serialization.SerializationException was unhandled by user code
  Message=Type 'System.Data.Entity.DynamicProxies.User_7B7AFFFE306AB2E39C07D91CC157792F503F36DFCAB490FB3333A52EA1D5DC0D' in Assembly 'EntityFrameworkDynamicProxies-Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
  Source=mscorlib
  StackTrace:
       at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
       at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
       at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
       at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
       at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
       at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
       at Skillscore.Web.Cloner.Clone[T](T source) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Extensions.Object.cs:line 62
       at Skillscore.Web.Models.Employer..ctor(User u) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Models\EF.Custom.cs:line 130

sigh... is everything always so hard?

* Update IV *

ok, so the problem above is that the Claim class has a navigator that points back to User - which explains why the above method indicates the type to be .User_[...] and implies I need to not only make the downward dependencies serializable, but also all of the paths back up! However, having done that I successfully clone the object but I'm now back to the issue in my original posting:

System.InvalidOperationException was unhandled by user code
  Message=Conflicting changes to the role 'User' of the relationship 'EF.ClaimUser' have been detected.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.EntityCollection`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
       at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
       at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClass5.<Add>b__4()
       at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
       at System.Data.Entity.DbSet`1.Add(TEntity entity)
       at Skillscore.Web.Controllers.AuthController.Register(String Company, String GivenName, String Surname, String Title, String Department) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Controllers\AuthController.cs:line 138

man. I need a hole in the head.

* Update V *

I don't know if the issue is the proxies or lazy loading, but after thinking about it a little, it seems that if I do a clone via serialisation, all the IDs for things that used to belong to the old object are now going to belong to the new one. I did do a .remove() first on the old object and if that has immediate effect then maybe there's something in the tracking that doesn't know about it. If it doesn't, then at one point there will be two things out there with the same ID... so I'm starting to lean towards @Jockey's idea of using object initialisers for the cloning...

7
  • Is user generated by EF? Sep 1, 2011 at 22:03
  • yes, and so is Employer and Claim
    – ekkis
    Sep 1, 2011 at 22:10
  • I think the answer's going to be trawling through the collections by hand and deeply copying the elements in them...
    – ekkis
    Sep 1, 2011 at 22:11
  • @Jockey, it was a step in the right direction (see Update III) but alas I'm not there yet
    – ekkis
    Sep 1, 2011 at 22:31
  • Instead of .Clone() try and object intializer and reset all the properties..lame I know but might work. cs.Add(new Claim { Property = c.Property }) Sep 1, 2011 at 22:36

5 Answers 5

35

If you want to serialize entities you can disable proxy creation before retrieving that object. You also need to eager load navigational properties if you want to serialize them as well.

To disable proxy creation in EF 4.1

dbContext.Configuration.ProxyCreationEnabled = false;

In EF 4

objectContext.ContextOptions.ProxyCreationEnabled = false;

eg:

var users = context.Users.Include("Claims").Where(/**/);
4
  • Will disabling proxy creation mean that all fields must be queried explicitly?
    – Travis J
    Jul 5, 2012 at 18:09
  • I was forced to disable ProxyCreation in order to make [ScriptIgnore] work. It seems though that I do not have to specify each field explicitly. If I var q = context.Set<TEntity>(); it will still give me all fields. For virtual relations, additional Includes must be used. What aspect of lazy loading will not work with ProxyCreation disabled?
    – Travis J
    Jul 6, 2012 at 18:02
  • @TravisJ EF will not be able to lazy load the navigation properties if you disable proxy creation.
    – Eranga
    Jul 7, 2012 at 1:28
  • @Eranga , i have one question. I didnt disable ProxyCreation and i just disabled Lazyloading alone. Serialization works fine for json but not for Xml and its complaining abt the proxy stuff. If dynamic proxies cannot be serialized, how come it work for json?
    – TechQuery
    Oct 7, 2015 at 17:50
1

Look into T4 Templates for Entity Framework, you can control how EF generates your entities, you will have to define that they are serializable in the T4 Template.

4
  • Here is a place to start. matthidinger.com/archive/2010/04/22/… Sep 1, 2011 at 21:18
  • Jockey, thanks for the suggestion. it's certainly interesting but I don't see how it helps me - see my update above.
    – ekkis
    Sep 1, 2011 at 21:54
  • Which proxy that is generated specifically. If you use the T4 Templates you can control everything about how the dynamically generated entities are created, so you would in that Template go defined those proxies or anything else you need to be serializable.? So I used T4 templates for my self to make the generated items from EF for example to be serializable and POCO style to be sent of WCF serialization. Sep 1, 2011 at 22:00
  • @Jockey, yes, I get that. the problem is I don't know the correct syntax to indicate that a property needs to be serializable... in other words when doing my deep copy, I don't want to make User serializable but I do need to make sure Claims get deeply copied. I've included the deep-copy code in a second edit above.
    – ekkis
    Sep 1, 2011 at 22:04
1

Turn off lazy loading and turn off proxy class creation. Anyway you still need to add the Serializable/DataContract attributes in order to make it serializable.

0

I had the same issue using Entity Framework 6 ( EF6 ). I fixed the issue by changing the T4 template and adding the line [Serializable] between the Using Directives and the Entity Class Opening line. Like so:

 <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
 [Serializable]
 <#=codeStringGenerator.EntityClassOpening(entity)#>

No other changes required.

1
  • another person answered the same way 4 years previous and the OP said it didn't help.
    – Matt
    Jan 19, 2017 at 15:51
0

As a Microsoft recommendation, you need to use DTO instead of EF entities to avoid serialization issues, consider also to use eager loading before transforming your entities to DTO.

One way to avoid serialization problems is to serialize data transfer objects (DTOs) instead of entity objects.

...

What happens if you add the corresponding navigation property [...]?

Unfortunately, this creates a problem when you serialize the models. If you load the related data, it creates a circular object graph.

One solution is to use DTOs, [...] Alternatively, you can configure the JSON and XML formatters to handle graph cycles.

EF Documentation - How to create a Web API application that uses Entity Framework for database persistence.

1
  • @ekkis, does my answer was helpful, still have issues or does this resolve your issues ?
    – Mselmi Ali
    Jul 26, 2019 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.