简体   繁体   中英

Entity Framework - Precision in queries

Very similar to this question: Why is Entity Framework passing a parameter as DECIMAL(5,0) for a column defined with NUMERIC(19)?

However, the answer given doesn't seem to align with my results.

I have a column, OrderEntryTimeUtc datetime2(0) over which the table is partitioned. Now, in order to actually use the partitioning, I need my queries to use the same type and precision.

But when I create my where clause:

o.OrderEntryTimeUtc > dateStart

where dateStart is a DateTime with the ms part set to 0, I get the following generated parameter:

@p__linq__2 datetime2(7)
@p__linq__2='2016-05-21 23:44:33'

How can I force EF to send a datetime2(0) ? Or how can I add a CONVERT to my query without having to write the whole query in SQL or create a view?

You can use the modelBuilder

modelBuilder.Entity<YourEntity>()
    .Property(p => p.OrderEntryTimeUtc)
    .HasColumnType("datetime2")
    .HasPrecision(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM