LINQ ToList () serialization error?

I have a LINQ β†’ Class file with the name MoviesDB.dbml, I added one table β€œMovie” to it, I created a class β€œMovies” in which I implemented all the methods.

The GetAllMovies method that gets the list, when linking the list, I get the following:

Type 'DyMvWebsite.Movie' in Assembly 'DyMvWebsite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 

Code for the method:

  public static List<Movie> GetAllMovies() { List<Movie> oMoviesList = new List<Movie>(); using (MoviesDBDataContext oMoviesDBDataContext = new MoviesDBDataContext()) { oMoviesList = oMoviesList.ToList<Movie>(); ; } return oMoviesList; } 

After that, I tried to put the [Serializable] attribute in the Movies class and tried to put it in the linq constructor file, but the same problem.

Edit:

After what Massimiliano Peluso suggested, I got a new error:

 Type 'System.Data.Linq.ChangeTracker+StandardChangeTracker' in Assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable. 
+4
source share
2 answers

try it

  MoviesDBDataContext.ObjectTrackingEnabled = false; 

before running the request.

If this still fails, then working around will create a serializable class similar to the Movie class and map the properties.

+3
source

Everything contained in the Movie class must be Serializable , as well as

Apply the SerializableAttribute attribute to the type to indicate that instances of this type can be serialized. The total server runtime throws a SerializationException if any type in the serializable graph does not have the SerializableAttribute attribute. Apply the SerializableAttribute attribute, even if the class also implements the ISerializable interface to control the serialization process. All public and private fields in a type marked with the SerializableAttribute attribute are serialized by default unless the type implements the ISerializable interface to override the serialization process. The serialization process by default excludes fields marked with the NonSerializedAttribute attribute. If a serializable type field contains a pointer, descriptor, or some other data structure specific to a particular environment and cannot be meaningfully restored to another environment, then you can apply the NonSerializedAttribute attribute to this field.

Additional Information:

http://msdn.microsoft.com/en-us/library/system.serializableattribute(v=VS.100).aspx

+2
source

Source: https://habr.com/ru/post/1389642/


All Articles