Serialization Issue while creating Deep Clone from POCOs generated by Entity Framework

I am using MVVM in our project. I am facing an issue relating to serialization in Entity Framework while creating Deep Clone of POCO Entities. My POCOs are inheriting from a Base class called EntityBase which is implementing INotifyPropertyChanged, IDataErrorInfo interfaces. If I specify Serializable attribute in POCOs then it started throwing exceptions that ViewModels should also have Serializable attribute specified.

Kindly suggest how can I solve the above issue.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, since you are implementing INotifyPropertyChanged, you probably have something like:

public event PropertyChangedEventHandler PropertyChanged;

This is an event, which means that it holds a pointer to a function of another class... In this case, probably the ViewModel (I can't really say, cause I didn't see your code).
You could have done in your ViewModel:

Model.PropertyChanged += ....

This means that when you try to serialize your model, it will also try to serialize all the things it have references to.

You can put an attribute on Events etc on your model (POCO) that will tell the serialization to ignore those properties (you also need to put it on privates since you are probably using BinaryFormatter) when serializing.
dineshwinsAuthor Commented:
Thanks for your reply Saragani. I have tried specifying the following two attributes on the events in my POCOs [NonSerialized], [System.Xml.Serialization.XmlIgnore] but it is not allowing me to do so. The Model.PropertyChanged + = .... event is declared in ViewModel class.
I am not sure how to deal with the above. Can you please suggest something as to where I can specify the XMLIgnore ?
Assuming you are using BinaryFormatter then [NonSerialized] should work.

Assuming that your ModelBase is defined like this, then you need to folliowing Bold Text to be added:

   public abstract class ModelBase : INotifyPropertyChanged
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        public ModelBase()

        protected void OnPropertyChanged(string propertyName)
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));

Any other event that you have in your models that is being registered by the view models also must be set as NonSerialized.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dineshwinsAuthor Commented:
Thanks Saragani. You made my day :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.