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

Filburt
  • 17,626
  • 12
  • 64
  • 115
pramod maurya
  • 123
  • 2
  • 2
  • 11
  • The .Net framework `System.Single` is mapped in SQL Server to `Real`. [Reference](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings) – Zohar Peled Nov 22 '17 at 07:45
  • 1
    In your case, you should also pay attention to the **non-null value** requirement. – Filburt Nov 22 '17 at 07:46

2 Answers2

0

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

-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)");
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116