Page 1 of 1
Cannot map Nullable type.
Posted: Thu 07 Aug 2014 14:16
by Albert
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!
Re: Cannot map Nullable type.
Posted: Tue 12 Aug 2014 05:49
by MariiaI
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.
Re: Cannot map Nullable type.
Posted: Tue 12 Aug 2014 08:09
by Albert
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)?
Re: Cannot map Nullable type.
Posted: Wed 13 Aug 2014 11:46
by MariiaI
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.
Re: Cannot map Nullable type.
Posted: Thu 14 Aug 2014 04:58
by Albert
Thank you for complete answer!
Re: Cannot map Nullable type.
Posted: Thu 14 Aug 2014 06:12
by MariiaI
If we can be of further assistance, please let us know.