none
Bug in nvarchar(1) conversion in DataContext designer

    Question

  • Hello all,

    I have some tables which use nvarchar(1) nullable fields. This gets translated on the LINQ entity (using the Visual Studio generator and dragging a table from the server explorer to the datacontext) to nullable char ("char?") properties for the entities, which might look correct at first glance, however, nvarchar(1) allows for empty strings, which char? doesn't, and LINQ will choke on any query with such field data.

    The workaround is a matter of making it a System.String on the entity, but it should be noted that nvarchar(1) entities are not necessarily one char in length or null (they can contain empty strings). In my application (made in Delphi, and currently making a port over to ASP.NET) this has caused a bunch of headaches, since some of those fields do mean different things with empty and null data for the application.

    Don't know if this should go on this forum though, it's not a bug with Linq per-se, but rather with the DataContext designer (and maybe SQL Metal? haven't tried it)

    Before I speak, I have something important to say.
    Thursday, January 08, 2009 12:51 AM

Answers

  • SQLMetal actually does generate the varchar(1) as String, so that is a workaround as well, if anyone else is running into this. We also have a bug tracking fixing this in the O/R Designer for the next version of VS.

    Thanks,

    Sarah


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, February 05, 2009 11:20 PM
    Moderator

All replies

  • Hi Javier,

    I have an add-in for VS2008 that adds a bunch of features to the L2S designer. One of those features is a menu that syncs the model with the underlying database. As of ver 1.56* (the latest version) it will map nvarchar(1) and varchar(1) to System.String instead of [as per the designer default] System.Char.  (* = Earlier versions of the add-in used the same type mapping as the designer, but as you correctly point out, using Char for nvarchar(1)/varchar(1) can result in errors for zero length strings)

    You can download it and get a free trial license from http://www.huagati.com/dbmltools/

    Best regards,
    Kristofer
    Thursday, January 08, 2009 9:00 AM
    Answerer
  • I'm gonna try it. Thanks a lot!
    Before I speak, I have something important to say.
    Thursday, January 08, 2009 9:23 AM
  • Wow, I love the addon, I was doing really wicked stuff with Sql Metal to update my datacontext's (the update tool was supposed to come with VS2008 SP1, but didn't) ... I will try to get my boss to buy it for me :-)

    Thanks a lot!

    Before I speak, I have something important to say.
    Thursday, January 08, 2009 9:42 AM
  • Javier Campos said:

    Wow, I love the addon, I was doing really wicked stuff with Sql Metal to update my datacontext's (the update tool was supposed to come with VS2008 SP1, but didn't) ... I will try to get my boss to buy it for me :-)


    Thanks a lot!

    Before I speak, I have something important to say.



    Hi Javier,

    Glad to hear that you like it! :)

    Best regards,
    Kristofer

    Tuesday, January 27, 2009 10:04 AM
    Answerer
  • SQLMetal actually does generate the varchar(1) as String, so that is a workaround as well, if anyone else is running into this. We also have a bug tracking fixing this in the O/R Designer for the next version of VS.

    Thanks,

    Sarah


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, February 05, 2009 11:20 PM
    Moderator
  • Hi Javier,

    I have an add-in for VS2008 that adds a bunch of features to the L2S designer. One of those features is a menu that syncs the model with the underlying database. As of ver 1.56* (the latest version) it will map nvarchar(1) and varchar(1) to System.String instead of [as per the designer default] System.Char.  (* = Earlier versions of the add-in used the same type mapping as the designer, but as you correctly point out, using Char for nvarchar(1)/varchar(1) can result in errors for zero length strings)

    You can download it and get a free trial license from http://www.huagati.com/dbmltools/

    Best regards,
    Kristofer

    It is not helpful to my issue, Thanks very much!
    Sunday, August 29, 2010 12:52 AM