Cannot map Nullable type.

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Albert
Posts: 20
Joined: Fri 06 Dec 2013 06:38

Cannot map Nullable type.

Post by Albert » Thu 07 Aug 2014 14:16

Hello,
I have an entity

Code: Select all

    public class Dictionary
    {
        public long? No { get; set; }
    }
and xml mapping

Code: Select all

  <Table Name="dictionary">
    <Type Name="TestDevart.Dictionary">
      <Column Member="No" Name="NO" IsPrimaryKey="true" />
    </Type>
  </Table>
When I try to query this entity

Code: Select all

var dictionary = DC.GetTable<Dictionary>().Take(10).ToList();
I get System.NotSupportedException with message "Cannot create key reader. Key type 'System.Nullable`1[System.Int64]' is not scalar type"
Why I can't map nullable type? Thanks!

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Cannot map Nullable type.

Post by MariiaI » Tue 12 Aug 2014 05:49

Albert wrote:I have an entity

Code: Select all

    public class Dictionary
    {
        public long? No { get; set; }
    }
and xml mapping

Code: Select all

  <Table Name="dictionary">
    <Type Name="TestDevart.Dictionary">
      <Column Member="No" Name="NO" IsPrimaryKey="true" />
    </Type>
  </Table>
You are defining the "No" entity property as Nullable (long?) and map it to the column which is a primary key ("IsPrimaryKey="true").
The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values and cannot contain NULL values.

Albert
Posts: 20
Joined: Fri 06 Dec 2013 06:38

Re: Cannot map Nullable type.

Post by Albert » Tue 12 Aug 2014 08:09

Is there any way to have association mapping for "child" entity, that has EntityRef, but doesn't have property marked as primary key (because my entity has only nullable properties)?

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Cannot map Nullable type.

Post by MariiaI » Wed 13 Aug 2014 11:46

No, it can't be done. The association can be made only between entities with the entity keys (both, parent and child).
In your case, you can add a column with ROWID data type to the table without primary key and make this column as primary key.

Please also refer to this post (it is about LINQ to SQL, but the same principles are used for LinqConnect):
http://stackoverflow.com/questions/4697 ... sociations

For more information about the entities without entity key:
In case when entity classes don't have an entity key, they are not recognized as entities. Such entities can still be queried, but aren't cached, and change tracking is not enabled for them. LinqConnect does not allow modifications of such entities - an exception is raised when such entity is created or deleted, and updates of such entity are ignored.
To avoid this situation, you could mark any property of entity class as an Entity key and re-generate the code. Please refer here.

Albert
Posts: 20
Joined: Fri 06 Dec 2013 06:38

Re: Cannot map Nullable type.

Post by Albert » Thu 14 Aug 2014 04:58

Thank you for complete answer!

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Cannot map Nullable type.

Post by MariiaI » Thu 14 Aug 2014 06:12

If we can be of further assistance, please let us know.

Post Reply