开发者

Linq to SQL "Cannot insert the value NULL" when NOT NULL!

开发者 https://www.devze.com 2023-03-15 23:42 出处:网络
I am massively in need to help. I have spent an entire day when I do not have even an hour to waste on a project. I have a Linq to SQL data layer and am doing a simple insert into the DB using the fol

I am massively in need to help. I have spent an entire day when I do not have even an hour to waste on a project. I have a Linq to SQL data layer and am doing a simple insert into the DB using the following code:

 using (var odc = new OrdersDataContext())
        {
            try
            {
                var id = GetNextOrderId(odc);
                order.ID = id;
                odc.tblOrders.InsertOnSubmit(order);
 开发者_如何转开发               odc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }

Due to other legacy code and this being a database that is running on SQL 2000 I cannot make database alterations, but this code was working fine at one point and then simply stopped working. It is saying that :

Cannot insert the value NULL into column 'ID', table 'Order'; column does not allow nulls. INSERT fails.

But as you can see from the code and what I can CLEARLY see as I step through the code, the ID column is getting a valid ID number. For some reason even though I am setting the ID field it is attempting to insert it as a null even though this code was working fine before. I've gone so far as to completely restore the database and rebuilt the data layer, but to no avail.

I'm so stumped on this and out of time that I'm beside myself.


So, the answer to this is very strange and I'm not 100% certain of why. Essentially what I did was completely rebuild the LINQ to SQL data layer (merely dragging and dropping the same tables in the designer) and then completely cleaning and rebuilding the entire solution. After that the code magically worked again. No code change required.

For some reason the code was improperly cached and that caused it to not be able to transfer the object to the data layer (is my guess). It seems like a strange error to get back in that case.

0

精彩评论

暂无评论...
验证码 换一张
取 消