LINQ to SQL problem with bit/booleans

I'm having an issue with LINQ2Sql where the value in the database is not getting to my code correctly. Everything is OK except for columns defined as a bit field in the database. In my code after the LINQ query, the value for bit fields is always false. What the heck is going on?

I've already tried deleting the table from the SQL desigener and then adding it back in, but that doesn't make a difference.

I look at the table data in SQL Server Mgmt Studio, and the values for my bit columns are correct: 1 for true, 0 for false. If I edit the table in Mgmt Studio, it has True/False in the right places. If I run my query in LINQ Pad, it returns the correct values. If I go into the debugger and grab the SQL from the LINQ results, I get the correct results when I run that!

Here is my LINQ code ...

var theplan = (from plans in _data.Plans where plans.PlanId.ToString() == sId select plans).FirstOrDefault();

Here are my table definitions ...

CREATE TABLE [dbo].[Things]( [ThingId] [uniqueidentifier] NOT NULL, [ValidEnvelope] [bit] NOT NULL, [Xmax] [float] NOT NULL, [Xmin] [float] NOT NULL, [Ymax] [float] NOT NULL, [Ymin] [float] NOT NULL, [IsValid] [bit] NOT NULL, CONSTRAINT [PK_Things] PRIMARY KEY CLUSTERED .... etc. CREATE TABLE [dbo].[Plans]( [PlanId] [uniqueidentifier] NOT NULL, [Name] [varchar](50) NOT NULL, [Created] [datetime] NOT NULL, [Modified] [datetime] NOT NULL, [ThingId] [uniqueidentifier] NOT NULL, [ViewStateId] [uniqueidentifier] NOT NULL, CONSTRAINT [PK_plans] PRIMARY KEY CLUSTERED ... etc.

Not that the Plans.ThingId column points to Things.ThingId as FK. The problem columns are Things.ValidEnvelope and Things.IsValid.

Here is the XML from the dbml file ....

<?xml version="1.0" encoding="utf-8"?> <Database Name="XXXX" Class="XXXXDbDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007"> <Connection Mode="AppSettings" ConnectionString="......" .... /> <Table Name="dbo.ViewStates" Member="ViewStates"> <Type Name="ViewState"> <Column Name="ViewStateId" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" CanBeNull="false" /> <Column Name="Xmax" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Column Name="Xmin" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Column Name="Ymax" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Column Name="Ymin" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Association Name="ViewState_Plan" Member="Plans" ThisKey="ViewStateId" OtherKey="ViewStateId" Type="Plan" /> </Type> </Table> <Table Name="dbo.Plans" Member="Plans"> <Type Name="Plan"> <Column Name="PlanId" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" CanBeNull="false" /> <Column Name="Name" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" /> <Column Name="Created" Type="System.DateTime" DbType="DateTime NOT NULL" CanBeNull="false" /> <Column Name="Modified" Type="System.DateTime" DbType="DateTime NOT NULL" CanBeNull="false" /> <Column Name="ThingId" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" CanBeNull="false" /> <Column Name="ViewStateId" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" CanBeNull="false" /> <Association Name="ViewState_Plan" Member="ViewState" ThisKey="ViewStateId" OtherKey="ViewStateId" Type="ViewState" IsForeignKey="true" /> <Association Name="Thing_Plan" Member="Thing" ThisKey="ThingId" OtherKey="ThingId" Type="Thing" IsForeignKey="true" /> </Type> </Table> <Table Name="dbo.Things" Member="Things"> <Type Name="Thing"> <Column Name="ThingId" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" CanBeNull="false" /> <Column Name="ValidEnvelope" Type="System.Boolean" DbType="Bit NOT NULL" CanBeNull="false" /> <Column Name="Xmax" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Column Name="Xmin" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Column Name="Ymax" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Column Name="Ymin" Type="System.Double" DbType="Float NOT NULL" CanBeNull="false" /> <Column Name="IsValid" Type="System.Boolean" DbType="Bit NOT NULL" CanBeNull="false" /> <Association Name="Thing_Plan" Member="Plans" ThisKey="ThingId" OtherKey="ThingId" Type="Plan" /> </Type> </Table> </Database>

-------------Problems Reply------------

Run SQL Server Profiler, check the exact query that's being generated, copy that exact query, and run it in Management Studio.

That procedure should definitely yield the same result. What I think you'll find, before even getting that far, though, is that you might, perhaps, not be seeing the query at all. It might be cached somewhere on the way, or the database you're connecting to in your project might not be the one you think it is (check which connectionstring is being used in the dbml-properties, and then check that string in the project's settings, and also check for potential overwrites in your web.config)

Found the problem in another part of the code. Stupid mistake.

Category:c# Views:0 Time:2009-12-04

Related post

  • LINQ to SQL - Problem with 1-to-1 association 2009-03-13

    In the L2S designer I have dropped a table and a view. I tried adding an association between the 2 on their primary keys. This should be a one-to-one relationship, so I set the cardinality property of the association as such. But, when coding I can't

  • Linq-To-SQL Problem Inserting Multiple Objects That Are The Same 2010-06-24

    Here is my object association: Summary -> Job -> Operator -> Job -> Operator So, I have a Summary object that contains a collection of Jobs, which has an Operator object (clock #, name, etc) When creating a new summary, I go through and c

  • LINQ to SQL Problem with table named 'System' 2010-11-12

    I have the unfortunate problem that a database I'm trying to use with LINQ to SQL has a table in it named 'System'. The side-effect of this is that there are HUGE namespace conflicts once the .dbml has been generated. Renaming the table is not an opt

  • Linq to SQL - Problem in Stored Procedure 2010-08-28

    I have storeProcesdure name called as prc_GetID, it's return value (ClientID) Here thr proc Code: Create PROCEDURE [dbo].[prc_GetID] (@ID VARCHAR(20)) AS SET NOCOUNT ON; SELECT ClientID FROM dbo.Clients WHERE [ID][email protected] I use above STORED PROCEDURE wit

  • Linq to Sql problem with converting nullable binary format 2011-02-22

    I am trying to get image from db (SQL server 2008) which are saved as binary nullable value using linq to sql query. My Query is: var news = from topic in db.PublicNews orderby topic.PostedDate descending select new PublicNewsDto() { Id = topic.Id, P

  • Linq to SQL Problem System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager 2010-06-09

    I have a really tricky thing going up here. My project has around 100 tables and they are all mapped by LINQ. Everything works fine in a dev and test environment. These enviroments are MS Win 2008 r2 servers with SQL 2008 sp1 databases. IIS and SQL a

  • LInq to SQl problem-Please help 2009-09-16

    I have the following data ProductId Description cost 12 test 0.0 12 test 8.8 12 test 7.9 27 asdf 9.0 27 asdf 2.0 27 asdf 2.0 I want the following result 12 test 0.0 / test8.8/test 7.9 27 asdf 9.0/asdf 2.0/ asdf 2.0 so far i could only come up with th

  • Insert linq-to-sql problem? 2010-02-14

    I'm playing around with the Northwind database and by reading some tutorials I see that Categories should have an Add method, but it ain't working. Something is missing and I would appreciate someone else who knows more for some feedback. public void

  • LINQ to SQL problem updating a property in a join query on select:: Explicit construction of entity type in query is not allowed 2011-04-27

    This is the query I'm trying to achieve (I know it is incorrect but it is here to illustrat exactly what i'm trying to achieve). Essentially upon join's Select I need to populated partical Class's CalendarEvent NextStartDate property. I tried creatin

  • LINQ-to-SQL Enumerable expression with Binary = null fails 2009-09-02

    This is a strange LINQ-to-SQL problem which can't evaluate as an Enumerable (in SQL) but I can evaluate client side. I think it's related to my testing Binary property as 'null'. I need to determine when my Job is complete, which means that all Files

  • Returning a single value with Linq to SQL 2009-10-09

    I'm learning Linq to SQL and I'm having trouble grasping it. I'm trying to simply return a single (boolean) value in C# with a Linq query. I want to see if the owner of a story would like an email notification sent when new comments are added. I woul

  • Provide binary download links where needed using ASP.NET MVC3 Linq to SQL, maintaining Deferred Loading 2011-07-06

    I am using a VARBINARY(MAX) column to store binary files in a SQL Server table. The other columns in the same table are used to store attributes about the file, such as a description and a time stamp indicating when the file was uploaded. I created a

  • Problem attaching an entity in LINQ to SQL 2009-02-05

    I'm trying to attach an entity in LINQ to SQL but it throws the following exceptionL: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported. <Table Name="dbo

  • Problem with Linq to SQL 2010-11-26

    I've been working with Linq to SQL for some time now, and I never had a problem ... until now. I have a method that send a query to the database, but different from the other times I did it, I cannot cast the result (be it to List or to Array). The m

  • Problem using LINQ to SQL with one DataContext per atomic action 2008-11-03

    I have started using Linq to SQL in a (bit DDD like) system which looks (overly simplified) like this: public class SomeEntity // Imagine this is a fully mapped linq2sql class. { public Guid SomeEntityId { get; set; } public AnotherEntity Relation {

  • LINQ-to-SQL + One-to-Many + DataBinding deleting problem 2008-12-21

    I use LINQ-to-SQL to load data from a database that has two tables in a one-to-many relationship (one Recipe has many Ingredients). I load a Recipe and LINQ retrieves Ingredient objects into an EntitySet that is binded into a ListBox. If I want to de

  • LINQ to SQL deployment problem 2009-03-10

    I have an asp.net application developed. It uses LINQ to SQL to access database, using the .dbml designer in Visual Studio 2008. We are installing the application on client, and they have decided to change the database name on their servers. Now, the

  • LINQ TO SQL :: Data Update Problem 2009-06-11

    i am developing a project (using 3-tier approach) in which i am using LINQ TO SQL... i want to update user... but i am facing some problem. it does not give me any error but also do not update the user detail here is the program sequence; in UpdatePr

  • LINQ to SQL DuplicateKeyException problem 2009-07-15

    I am using LINQ2SQL and I have a table called Customers with three columns CustmerID, CustomerCode, CustomerName CustmerID is Primery Key(and Identity=yes) and CustomerCode is just UniqueKey. When I am updating this table using LINQ to SQL with dupli

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.183 (s). 11 q(s)