2

I have Created class

public class UserAppearanceSettings
{
    [Key]
    public long Id { get; set; }
    public string UserName { get; set; }
    public string SkinName { get; set; }
    public string FontName { get; set; }
    public float FontSize { get; set; }

}

my sql table script

CREATE TABLE [dbo].[UserAppearanceSettings](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NULL,
[SkinName] [nvarchar](50) NULL,
[FontName] [nvarchar](50) NULL,
[FontSize] [float] NULL
) ON [PRIMARY] 

and getting error

The 'FontSize' property on 'UserAppearanceSettings' could not be set to a 'System.Double' value. You must set this property to a non-null value of type 'System.Single'.

What datatype should I use in sqlserver for Float

2
  • The .Net framework System.Single is mapped in SQL Server to Real. Reference Commented Nov 22, 2017 at 7:45
  • 1
    In your case, you should also pay attention to the non-null value requirement. Commented Nov 22, 2017 at 7:46

2 Answers 2

0

I believe it should be double. https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings

Sign up to request clarification or add additional context in comments.

5 Comments

Instead of answering such questions, it is better to flag or vote to close as a duplicate.
Well, yes, you are right. I am kind of new here.
I understand. That is exactly the reason I am telling you, before you get downvoted a lot.
@PatrickHofman I want to change sql datatype not C#
Then use the appropriate data type. See the duplicate referenced above your question: stackoverflow.com/questions/425389/…
-1

Use fluent API to instruct entity framework to behave differently than it would behave by convention. Do this in your override of DbContext.OnModelCreating.

If you want to tell entity framework that every float in your DbSet classes should have SQL Data type Float(size, d) use ConventionPrimitivePropertyConfiguration

public override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Treat every float as SQL FloatType(53:
    modelBuilder.Properties<float>
        .Configure(floatType => floatType.HasColumnType("float(53)");

    base:OnModelCreating(modelBuilder);
}

If you only want some float properties to have the float type use HasColumnType

modelBuilder.Entity<UserAppearanceSettings>()
    .Property(setting => setting.FontSize)
    .HasColumnType("float(53)");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.