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();
intyou should useshortas type forCountproperty.shortI think. If you don't want to do casts throughout your code you could add an unmappedintproperty that gets/sets yourCountproperty and use that in your code instead.enums. Posted new question here