简体   繁体   English

LINQ ToList()序列化错误?

[英]LINQ ToList() serialization error?

I have a LINQ -> Class file named MoviesDB.dbml, I have added one table "Movie" to it, i have created a class "Movies" in which i have implemented inside all the methods. 我有一个名为MoviesDB.dbml的LINQ - > Class文件,我已经添加了一个表“Movie”,我创建了一个类“Movies”,其中我已经在所有方法中实现了。

Method GetAllMovies which gets a List, when binding the list i get the following: 获取List的方法GetAllMovies,绑定列表时我得到以下内容:

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

The 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 have tried putting the [Serializable] attribute in the "Movies" class and tried putting it in the linq designer file too, but same problem. 之后我尝试将[Serializable]属性放在“Movies”类中并尝试将它放在linq设计器文件中,但同样的问题。

Edit: 编辑:

After following what 'Massimiliano Peluso' suggested, i received a new error: 按照'Massimiliano Peluso'的建议,我收到了一个新的错误:

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. 

Try this out 试试吧

  MoviesDBDataContext.ObjectTrackingEnabled = false;

before firing the query. 在触发查询之前。

If this still fails, then the work around will be create serializable class similar to Movie class, and map the properties. 如果这仍然失败,那么解决方法是创建类似于Movie类的可序列化类,并映射属性。

Everything contained in the Movie class must be Serializable as well Movie类中包含的所有内容也必须是Serializable

Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. 将SerializableAttribute属性应用于类型以指示可以序列化此类型的实例。 The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied. 如果要序列化的对象图中的任何类型未应用SerializableAttribute属性,则公共语言运行库将抛出SerializationException。 Apply the SerializableAttribute attribute even if the class also implements the ISerializable interface to control the serialization process. 即使该类还实现了ISerializable接口来控制序列化过程,也应用SerializableAttribute属性。 All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process. 默认情况下,序列化由SerializableAttribute标记的类型中的所有公共和私有字段,除非该类型实现ISerializable接口以覆盖序列化过程。 The default serialization process excludes fields that are marked with the NonSerializedAttribute attribute. 默认序列化过程会排除使用NonSerializedAttribute属性标记的字段。 If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply the NonSerializedAttribute attribute to that field. 如果可序列化类型的字段包含特定于特定环境的指针,句柄或某些其他数据结构,并且无法在不同环境中进行有意义的重构,则可能需要将NonSerializedAttribute属性应用于该字段。

more info: 更多信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM