LINQtoSQL 和序列化

发布于 2024-12-01 14:57:39 字数 1278 浏览 1 评论 0原文

我是 LINQtoSQL 的新手。我选择学习它是因为我没有太多用 SQL 编写存储过程的经验,并且认为 LINQtoSQL 可能是进行数据访问的另一种方法。不管怎样,我创建了一个简单的 ASP.NET 应用程序,其中包含几个简单的 Web 表单,用于收集用户的一些数据。

接下来,我将 LINQtoSQL 数据对象(dbml 文件)添加到我的项目中。将其连接到我的 SQL 数据库中的数据源,并允许 OR/设计器为我创造一个实体。一切似乎美好得令人难以置信。直到我开始将在网络表单中收集的数据保存回数据库。这就是我开始了解 LINQtoSQL 序列化问题的地方。

具体来说,我的网络表单分多个步骤收集数据。与任何 ASP 开发人员一样,我在经历页面生命周期时将这些数据存储在 ViewState 中。最后,一旦我的 ViewState 拥有所需的所有数据,请点击提交按钮以使用 LINQtoSQL 实体对象将其发送到数据库。这就是我开始收到序列化异常的地方:

System.Runtime.Serialization.SerializationException: Type 'System.Data.Linq.ChangeTracker+StandardChangeTracker' in Assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken =b77a5c561934e089' 未标记为可序列化。

我在网上查了一下。我知道我的唯一一个 LINQtoSQL 实体(我从 ViewState 分配的实体)是可序列化的,因为它是用所需的 DataContract 和 DataMember 属性装饰的。经过大量阅读后,我尝试了 Brain Orrell 在以下链接中提供的解决方案,该解决方案要求提供您自己的序列化器。对于我想要实现的目标(将一段数据简单写入数据库)来说似乎非常复杂,并且序列化异常仍然没有消失。尽管奇怪的是,我的实体现在已正确写入数据库:

http://borrell.parivedasolutions.com/2008/02/linq-to-sql-updating-in-aspnet-right.html

我的问题是将 ViewState 数据传递到 LINQtoSQL 实体以便将其写入数据库的正确且简单的方法。如果有人知道一个具体但很好的例子可以遵循,我将不胜感激。否则,我可以发布我自己项目中的代码,我们可以从那里开始。

如果提前的话谢谢。

菲克

I am a newbie to LINQtoSQL. I opted to learn it because I didn't have much experience writing stored Procedures in SQL and thought that LINQtoSQL might be an alternative way to do data access. Anyway, I created a simple ASP.NET application with a couple of simple web forms that collect some data from the user.

Next I added a LINQtoSQL Data object (a dbml file) to my project. Connected it to a DataSource in my SQL database and allowed the OR/Designer to do it' magic about creating an entity for me. All seemed too good to be true. Until, I got to the saving the data that I have collected in my web forms back to the database. That is where I began to learn about the serialization problems with LINQtoSQL.

Specifics are that my web forms collect data in multiple steps. Like any ASP developer, I store this data in the ViewState as I go through Page LifeCycles. Finally, once my ViewState has all the data that is required, hit the submit button to send it to the database using LINQtoSQL entity object. And this is where I start getting serialization exception:

System.Runtime.Serialization.SerializationException: 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.

I looked around on the web. I know that my one and only LINQtoSQL entity (that I am assigning from my ViewState) is seiralizable since is it decorated with required DataContract and DataMember attributes. After much reading I tried a solution given by Brain Orrell at the following link that calls for providing your own serializer. It seems very complicated for what I am trying to achieve (a simple write of a piece of data to a database) and the serialization exceptions still didn't go away. Although, strangely, my entity is correctly written to the database now:

http://borrell.parivedasolutions.com/2008/02/linq-to-sql-updating-in-aspnet-right.html

My question is, what is correct and a simple way to pass the ViewState data to a LINQtoSQL entity so it could be written to the database. And if some one knows of a specific but good example to follow, I will appreciate it. Otherwise I can post code from my own project and we can go from there.

Thanks if advance.

Fike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2024-12-08 14:57:39

柯克的评论实际上回答了你的问题。我只是再给你一个建议 - 如果你是 ORM 中的“白板”,请尽快忘记使用 LinqToSql 的不愉快经历并尝试新的实体框架。它是更加健壮、灵活的解决方案(具有类似的视觉设计器和围绕它的“魔法”),但它允许您使用 POCO 作为实体类 - 您可以使其可序列化,或以任何其他需要的方式调整它。

Kirk's comment is in fact answer to your question. I just give you one more suggestion - if you are "tabula rasa" in ORMs, quickly forget about unpleasant experience with LinqToSql and try new Entity Framework. It is much more robust,flexible solution (with the similar visual designer and "magic" around it), but it allows you to use POCOs as your entity classes - you can make it serializable, or tweak it in any other way needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文