简体   繁体   English

Linq-To-SQL在插入而不是更新时无效转换

[英]Linq-To-SQL Invalid Cast on insert, not on update

Hey guys, I'm having a weird time with Linq-To-SQL 大家好,我在Linq-To-SQL上度过了一段奇怪的时光

I'm building a postit system that allows people to make postits and attach them to multiple objects. 我正在构建一个postit系统,该系统允许人们制作postit并将其附加到多个对象上。 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. 为此的数据库是一个表,该表具有postit本身(id,消息等)和一个链接表,其中包含postit所附加的记录。

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. 当我从数据库中检索对象(使用Linq-To-SQL)时,进行一些数据更改并再次提交,我丝毫没有遇到任何麻烦。 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. 但是,当我尝试制作一个新的时,我在datacontext的Submitchanges方法中遇到一个异常:指定的Cast无效。

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. 我使用上面的代码,似乎无法理解我在做什么。

help anyone? 帮助任何人?

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). 如果我猜到了,可能是CreatedOn的值不能转换为数据库中的有效DateTime(由于区域性设置)。 That would explain why updating goes OK - you're not changing the value of CreatedOn here. 那可以解释为什么更新可以进行-您在这里没有更改CreatedOn的值。 You do, however, when inserting a new entity. 但是,在插入新实体时会这样做。

Edit: 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. 另外,请确保阅读此线程 ,主题启动程序最终指向第一个线程,其中提到这是Linq2Sql中的一个明显错误。

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

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