EF Core 6 Decimal precision warning - entity-framework-core

After upgrading to EF Core 6, I have this annoying warning here when adding a migration:
No store type was specified for the decimal property '{property}' on entity type '{entityType}'. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values in 'OnModelCreating' using 'HasColumnType', specify precision and scale using 'HasPrecision', or configure a value converter using 'HasConversion'.
which is defined here: https://github.com/dotnet/efcore/blob/main/src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs
I am not sure to understand the purpose of this warning because on entities with the tag [Keyless] as they do not live in the database. Also adding an attribute such as [Column(TypeName = "decimal(28, 6)")] doesn't seem to clear the warning on keyless entities.

Apparently there's a new attribute for EF Core 6:
using Microsoft.EntityFrameworkCore;
...
[Precision(18, 2)]

Related

Getting Precision And Scale In Entity Framework Core

Is there a way in Entity Framework Core 3 to read the precision and scale specified on a column?
For example if I have:
[Column(TypeName = "numeric(5, 2)")]
Is there a way in code to access the precision value of 2 and scale value of 5?
I can access the "numeric(5, 2)" as a string but don't want to have it to parse that to get the 2 and 5.
The code I am using is this:
myModel.FindEntityType(myEntityType).FindProperty(myPropertyName).GetRelationalTypeMapping();
I can actually see whilst debugging, the scale and precision specified in the Parameters collection returned by the above code but as Parameters is protected there is no way to access it in code.
This was possible in Entity Framework (.Net Framework) using the EdmProperty class.
Thanks
Tristan.

NPGSQL date time array

I'm attempting to use the timestamp[] field type in Postgres with NPGSQL, so that I can use the DateTime[] type in my Entity Framework models.
I've added this to my EF code first model.
[Column("HostUnavailableDates", TypeName = "timestamp[]")]
public DateTime[] HostUnavailableDates { get; set; }
I've created a migration and the database has updated successfully.
However I am getting this error when executing transactions with the model.
Message: System.InvalidOperationException : The property 'HostApplication.HostUnavailableDates' could not be mapped, because it is of type 'DateTime[]' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
I've followed this answer which does not use any type of ignoring of the property. Is there something I need to do for the DateTime type in addition to what I'm currently doing?
Is DateTime in fact not supported at all in this case?
I'm using EF Core.
I'm assuming you're using Entity Framework 6.x. If that's the case, then arrays simply aren't supported, you'll have to switch to Entity Framework Core.

EF CORE How to Change NVACHAR to VARCHAR

In Entity Framework 6.X It was possible to change the default database type by doing this:
modelBuilder.Properties<string>().Configure(c => c.HasColumnType("varchar"));
In EF Core how can i do that?
The method Properties() doesn´t exists.
Since EF Core 6, based on github repository of entity framework (see here) :
previous versions of EF Core (before EF Core 6) require that the
mapping for every property of a given type is configured explicitly
when that mapping differs from the default. This includes "facets"
like the maximum length of strings and decimal precision, as well as
value conversion for the property type.
In the class that inherits from DbContext override ConfigureConventions
protected override void ConfigureConventions(
ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder
.Properties<string>()
.AreUnicode(false)
.HaveMaxLength(1024);
}
Thus all string properties can be configured to be ANSI (instead of Unicode) and have a maximum length of 1024, hence mapping string to nvarchar changes to varchar when migrations are applied to the database.

How get EF GetValidationErrors to validate decimal magnitude?

Currently, in EF 6.1.3, if a decimal is declared in Code First with HasPrecision(6,2) and the value is 1234567.0 then GetValidationErrors does not detect any error, and SaveChanges fails.
I don't want to custom validate every entity being written to the database and would like to somehow get GetValidationErrors to validate the magnitude of a decimal about to be written to the database that has precision declared.
Is this possible? May be I'm missing something...Thx!
You change your property precision for example in your DbContext like that:
modelBuilder.Entity<AnEntity>().Property(e => e.property).HasPrecision(14, 12);
You can also disabling the automatic validation on Save and create your custom data annotation for the validation:
MyDbContext.Configuration.ValidateOnSaveEnabled = false;
You can create your custom EF validator as described in my post below:
Entity Framework UI Validation using WinForms

ef4 poco double/float dbtype mapping

I have an exiting database with couple of float/double fields, I chose to use EF4.1 for my DAL/ORM, but I am having problems when reading/saving data to his type of fields. There is no model, just using model builder to configure the entities.
First there was a rounding problem, I think typical of floating point math, so because all of the precision were already set as 2 decimals, I switched to using decimal type on the c# (EF) side, but now when I am trying to get an entity, I get an exception saying I am in trouble.
The 'xxxkg' property on 'YYY' could not be set to a 'Double' value.
You must set this property to a non-null value of type 'Decimal'.
xxxkg is float null on the database, and the value is 10 in the specific case, and in the class it is
public decimal? xxxkg { get; set; }
The question is how to deal with dbtype float/double variables with EF4 (poco)?
You cannot define your property as decimal if it is double in database. EF does not type conversion (and doesn't allow you defining your own) and double value cannot be assigned to decimal variable so populating your class from read record will fail. You must use double.