Click here to Skip to main content
15,867,686 members

Comments by F-ES Sitecore (Top 200 by date)

F-ES Sitecore 31-May-21 12:34pm View    
You should learn to use the debugger and step through your code a line at a time to get a better idea about what is happening.
F-ES Sitecore 14-Apr-21 11:05am View    
Do you want to give them new IDs when you clone them in the DB? If so I'd try just detatching them, setting the ID to 0 then try to Add to the relevant table\dbset. If you want to duplicate the address objects you'd probably need to do the same.
F-ES Sitecore 14-Apr-21 6:39am View    
What is the main reason you are trying to close these objects? Once you've made clones of them you won't be able to call Save or whatever as they are no longer attached to a database context so you would need to attach them first. If you're just looking to modify the data and re-save it then you can get the entities as "no tracking" or non-attached entities, make the changes you want, re-attach them then save the context. I'm sure there is a better solution to your actual problem than cloning the objects.
F-ES Sitecore 14-Apr-21 6:08am View    
Is testObj.PrimaryAddress null in the line that throws the exception?
F-ES Sitecore 13-Apr-21 22:02pm View    
Actually now I think about it as these are EF entities the problem might be that you're using proxy objects in EF. Try disabling proxy generation

https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.infrastructure.dbcontextconfiguration.proxycreationenabled?view=entity-framework-6.2.0

and if the clone works after that then that's your problem. Either leave proxies off or if you want to close the proxied object you won't be able to use memberwiseclone at all, you'll literally need to copy the properties across from the original to a new object you crated. If you're using something like AutoMapper then it can do that for you, or you can use reflection

Reflection example

or just manually copy properties but that's pretty tedious. You can just re-get the items from the database, that's another option.