When I try to set a Linq-to-sql-object to my viewstate I will get a runtime exception that says that I must make the object serializable.
I have googled on my problem but can not find a solution on my problem. Is it possible to make these objects serializable? These objects are auto-generated so its impossible to just add a [Serializable]-attribut before the class and I cant find any settings for this in the particular dbml-file.
Thanks in advance!
In order to put it in Viewstate, the class must be attributed [Serializable]. Even if you could simple put that attribute on class you’re getting from the database, I’d advise against it.
What you need to do, and should do anyway, is define classes that represent the data you want to put in your Viewstate. You then fill these with the data from the database, and use them in Viewstate as needed. This not only solves your problem, but separates your view objects from your database objects so that you can change your Viewstate objects if you want, or change how you get data from the database as long as it can still populate your Viewstate objects.
See separation of concerns here: http://en.wikipedia.org/wiki/Separation_of_concerns
Edit:
It looks like the generated class is a partial class, so I just tested creating another partial class with the same name and marking it serializable. I was able to serialize the class and it included properties from both partial classes. After more tests, I started running into problems, so while it may be possible, it’s definitely as reliable as maintaining your own serializble classes.