Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9106989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:27:59+00:00 2026-06-17T02:27:59+00:00

Is it possible to force Entity Framework Code First to use floats (or doubles)

  • 0

Is it possible to force Entity Framework Code First to use floats (or doubles) in C# entities, and map them to decimals in SQL Server? There would need to be a conversion, and there could be precision loss going from decimal to float or double, but assuming that is avoidable due to the particular domain, will EF let you do it?

For example, I’d like to have

public class House
{
    ...
    public float Width { get; set; }
}

Map to

CREATE TABLE [dbo].[Houses] (
    ...
    [Width] [decimal](12, 4) NOT NULL
)

Can this be done with EF attributes, the Fluent API, or by some other means?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:28:00+00:00Added an answer on June 17, 2026 at 2:28 am

    No, it is not possible to create such a mapping. The decimal type on SQL Server and float or double on .NET are incompatible. That’s what the exception says when you try a mapping like

    modelBuilder.Entity<House>()
        .Property(h => h.Width)
        .HasColumnType("decimal"); // Does not work !
    

    Unfortunately, EF does not support any mappings between different primitive types. You are forced to use a type in .NET that is compatible with the column type in SQL Server.

    A possible (not very nice) workaround is to use two properties in your model class and map only one to the database:

    public class House
    {
        // ...
    
        private decimal _width;
        public decimal Width // mapped to database column as decimal
        {
            get { return _width; }
            set { _width = value; }
        }
    
        [NotMapped] // <- not mapped to a database column
        public float WidthAsFloat
        {
            get { return (float)_width; }
            set { _width = (decimal)value; }
        }
    }
    

    The type casts can throw exceptions if the source numbers do not fit into the target type, so there might be a more sophisticated conversion necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Framework and Code First to map to a legacy database
Is it possible to force a column in a SQL Server 2005 table to
In SQL Server 2000/2005, Is it possible to force the default value to be
is there a possible bit of code (php, java, css, html) that will force
Possible Duplicate: Entity Framework with NOLOCK I'm using EF4 and .Net 4 to load
Is it possible to force latex to use the slash notation for a fraction
Is it possible to force History.js - https://github.com/browserstate/History.js/ - to use the hash URLs
Is it possible to force jquery use CSS3 transitions for its fadeIn fadeOut effects?
Possible Duplicate: force base class to use its own method and not overrided method
Is it possible to force gcc use int instruction for all the system calls,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.