Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal type should be NUMERIC in Sqlite #18756

Closed
AdamJachocki opened this issue Nov 5, 2019 · 6 comments
Closed

Decimal type should be NUMERIC in Sqlite #18756

AdamJachocki opened this issue Nov 5, 2019 · 6 comments

Comments

@AdamJachocki
Copy link

Now, when one does a migration in Sqlite, all decimal types create TEXT data column in Sqlite database. I think they should create NUMERIC data type according to this (table in 3.1.1 section): https://www.sqlite.org/datatype3.html -> look at DECIMAL(10,5) data type. It should be represented as NUMERIC.

@ajcvickers
Copy link
Member

/cc @bricelam

@bricelam
Copy link
Contributor

bricelam commented Nov 5, 2019

NUMERIC is a type affinity. It means SQLite will prefer REAL (64-bit floating-point values) or INTEGER (64-bit integer values) as the on-disk format.

In .NET, decimal represents a 128-bit floating-point value. Coercing these to 64-bit values would be lossy so we persist them as TEXT values.

@bricelam
Copy link
Contributor

bricelam commented Nov 5, 2019

If you don't need the precision, but would still like to work with decimal types in your domain model, you can use a value converter:

modelBuilder.Entity<MyEntity>().Property(e => e.MyDecimalProperty)
    .HasConversion<double>()
    .HasColumnType("NUMERIC");

@AdamJachocki
Copy link
Author

I tried to do this in my db context:

protected override void OnModelCreating(ModelBuilder builder)
{
	//Debugger.Launch();
	base.OnModelCreating(builder);

	builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
        //in the above line, I am able to map my model and Ignore fields that I don't want to be in database

	if (DbInitializer.DatabaseType == DbType.Sqlite)
	{
		foreach (var entityType in builder.Model.GetEntityTypes())
		{
			var ignoredNames = entityType.GetIgnoredMembers();

			var props = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(decimal) && !ignoredNames.Contains(p.Name));
			foreach (var prop in props)
			{
				builder.Entity(entityType.Name)
					.Property(prop.Name)
					.HasColumnType("NUMERIC");
			}
		}
	}
}

So, as you can see, I want to set column type to numeric, only if DBMS is Sqlite and clr type is decimal. But this does not work. Created column type is still TEXT.

@ajcvickers
Copy link
Member

@AdamJachocki I am not able to reproduce this. Please post a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

@ajcvickers
Copy link
Member

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants