I mapped an int
property to smallint
in SQL Server. When I query the database, I get the following exception:
InvalidOperationException: An exception occurred while reading a database value for property 'Tag.Count'. The expected type was 'System.Int32' but the actual value was of type 'System.Int16'
I want to do it this way because if I use a short
on the Entity, I end up having to write extra code to cast short
to int
.
Snapshot of relevant code:
public class Tag
{
public int Count { get; set; }
//Other properties
}
//In DbContext
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Tag>().Property(m => m.Count).HasColumnType("smallint");
}
//query
var tags = await context.Tags.ToArrayAsync();
Change int
to int16
as SMALLINT
is 16 bits and int
is 32 bits.
So 32 bits can't be converted to 16 bits. You can use short
datatype also.
public class Tag
{
public int16 Count { get; set; }
// or,
/* public short Count { get; set; } */
//Other properties
}
//In DbContext
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Tag>().Property(m => m.Count).HasColumnType("smallint");
}
//query
var tags = await context.Tags.ToArrayAsync();