Linq-To-SQL在插入而不是更新时无效类型转换 [英] Linq-To-SQL Invalid Cast on insert, not on update

查看:40
本文介绍了Linq-To-SQL在插入而不是更新时无效类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我在Linq-To-SQL上度过了一段奇怪的时光

Hey guys, I'm having a weird time with Linq-To-SQL

我正在构建一个postit系统,该系统允许人们发布postit并将其附加到多个对象上.用于此目的的数据库是一个表,该表具有postit本身(id,消息等)和一个链接表,该表保存postit所附加的记录.

I'm building a postit system that allows people to make postits and attach them to multiple objects. The database for this is a table that has the postits themselves (id, message, etc) and a linkstable which holds the records to which a postit is attached.

我正在经历的奇怪的事情如下.当我从数据库中检索对象(使用Linq-To-SQL)时,进行一些数据更改并再次提交,我丝毫没有遇到任何麻烦.但是,当我尝试制作一个新的时,我在datacontext的Submitchanges方法中遇到了一个异常:指定的Cast无效.

the weird thing I'm experiancing is the following. When I retrieve an object from the database (using Linq-To-SQL), do some data changing and submit it again, I experience no trouble whatsoever. Yet, when I try to make a new one I get an exception in the Submitchanges method in the datacontext: Specified Cast is not valid.

我一直在网上寻找信息,而且大多数情况下都涉及到映射的某些更改,但事实并非如此,因为我可以进行更新而不会出现任何问题.

I've been looking on the web and mostly it involves some change in the mapping, but this shouldn't be the case as I can update without any problems.

T_PostIt np = new T_PostIt();
                np.CreatedOn = DateTime.Now;
                np.CreatedBy = Request.ServerVariables["REMOTE_USER"].ToString();
                np.MarkedForDeletion = false;
                np.Message = txtNewPostitMessage.Text;
                np.ModifiedBy = Request.ServerVariables["REMOTE_USER"].ToString();                    
                foreach (int i in ServerIds)
                {
                    T_PostIt_Link pil = new T_PostIt_Link();
                    pil.LinkType = 'S';
                    pil.LinkID = i;
                    pil.MarkedForDeletion = false;
                    np.T_PostIt_Links.Add(pil);        
                }
                dc.T_PostIts.InsertOnSubmit(np);
                dc.SubmitChanges();

我使用上面的代码,似乎无法理解我在做什么.

I use the above code and can't seem to get what I'm doing wrong.

帮助任何人吗?

推荐答案

您是否尝试过一次一个地更新属性,然后将更改保存回数据库?可能只有在更改了一个特定值时,更新实体才会失败.如果我猜想,可能是 CreatedOn 的值无法转换为数据库中的有效 DateTime(由于区域性设置).这可以解释为什么更新可以进行-您无需在此处更改 CreatedOn 的值.但是,在插入新实体时会这样做.

Have you tried updating the properties one by one, and then save the changes back to the database? It could be that updating the entity only fails when one specific value has changed. If I may guess, it could be that the value of CreatedOn cannot be cast to a valid DateTime in the database (due to culture settings). That would explain why updating goes OK - you're not changing the value of CreatedOn here. You do, however, when inserting a new entity.

也许这个线程,主题启动程序最终指向第一个线程,提到它是Linq2Sql中的一个明显错误.

maybe this is the problem you're facing. Also, be sure to read this thread, where the topic starter eventually points to the first thread mentioning that it is an apparant bug in Linq2Sql.

这篇关于Linq-To-SQL在插入而不是更新时无效类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆