Are Linq to sql objects serializable for session state?

后端 未结 3 1787
猫巷女王i
猫巷女王i 2021-01-03 05:32

Without going into whether this is a good or bad idea:

Is it possible to store a LINQ-to-SQL domain object in the ASP.NET Session, when the session is ou

相关标签:
3条回答
  • 2021-01-03 05:47

    Serialize them using the datacontractserializer before storing in session or anything else that may want to serialize... Recently discussed here:

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/81c84ff4-059b-474f-9c69-b8c59027fd48

    0 讨论(0)
  • 2021-01-03 05:52

    To use the binary formatter (like SessionState use I believe), you would need to generated your code yourself from the DBML (I do that currently with a Linq2Sql T4 template).

    The following need to be mark as [NonSerialized]:

    • EntityRef
    • EntitySet
    • All events (you will need to think outside the box to do this, exercise for reader)

    Also the constructor logic need to be moved to OnCreated. You must also make sure OnCreated is called when deserializing so the object can be useful again. The is done with the [OnDeserializing] attribute.

    0 讨论(0)
  • 2021-01-03 06:04

    I would believe that you would need to mark your objects as being serializable. I'm not sure if there is way to do this for all the generated objects, but for those that you are putting into session, you could create a partial class (e.g. of Lion) and give it the Serializable attribute.

    0 讨论(0)
提交回复
热议问题