How do I map a char property using the Entity Framework 4.1 "code only" fluent API?
Asked Answered
I

5

48

I have an object that has a char property:

public class Product
{
    public char Code
    {
        get;
        set;
    }
}

Entity Framework doesn't seem to be able to map chars (this field is missing from the database when I create the database schema from my model objects). Is there anyway I can map the char (e.g. to a string) using the fluent API? I don't want to change the model objects as they are part of a legacy shared library.

Isomorph answered 20/7, 2011 at 10:55 Comment(0)
H
75

Char is not valid primitive type for entity framework = entity framework doesn't map it. If you check CSDL reference you will see list of valid types (char is not among them).

Database char(1) is translated as string (SQL to CSDL translation). Char is described as non-unicode string with fixed length 1.

The only ugly option is second mapped property using string and your char non-mapped property will just use string[0] from that property. That is just another example how some simple type mapping or converters are damn missing in EF.

Heinrike answered 20/7, 2011 at 13:26 Comment(0)
B
34

In Fluent API you can specify the database column data type using the HasColumnType method like this:

modelBuilder.Entity<Product>()   
.Property(p => p.Code)   
.HasColumnType("char");

According to Andre Artus' answer here, HasColumnType is available in EF4.1.

For those using Data Annotations, the ColumnAttribute can accomplish the same thing.

[Column(TypeName="char")]
public string Code { get; set; }
Bawdy answered 8/2, 2015 at 23:7 Comment(5)
And if you want a single char, add attribute [StringLength(1, MinimumLength = 1)].Sequoia
[StringLength(1)] will be enough for varchar(1) in EF 6.1.3Hathaway
Another option with fluentApi: modelBuilder.Entity<Product>().Property(p => p.Code).HasMaxLenght(number_you_want).IsFixedLenght(); and this will produce char(number_you_want)Nanceenancey
IsFixedLength() change datatype from nvarchar to nchar, and for char ?Sealskin
ShortName is char(3) in database, and string property: builder.Property(p => p.ShortName).HasColumnType("char").IsFixedLength().IsUnicode(false).HasMaxLength(3).IsRequired().HasColumnOrder(order++);Sealskin
S
2

[Column( TypeName = "char(1)" )]

works for me with EF core 3.1.4

Spot answered 26/6, 2020 at 19:14 Comment(1)
Yes, but remember that this will not hold a single Unicode char.Timbered
Q
1

I have tried all the ways I have imagined and I must say the the accepted answer is the unique way to solve the problem of the char type as far as I know.

The char type isn't available for its use in EntityFramework.

Fluent API is included in this restriction.

If you try to put a char on the Property(p => p.MyCharProperty) will give you an Exception.

That means that char properties aren't available for Fluent API nor Attributes.

The easiest solution is this (as proposed by Ladislav Mrnka).

public class Product
{
    public char Code { get; set; }

    [Column("Code", TypeName="char")]
    [MaxLength(1)]
    public string CodeString
    {
         get { return Code.ToString(); }
         set { Code = value[0]; }
    }
}

One note: you can't put the property private, protected or internal. Must be public.

Fluent API version would be like this.

public class Product
{
    public char Code { get; set; }

    //We need the property but we will use the Fluent API to replace the attributes
    public string CodeString
    {
         get { return Code.ToString(); }
         set { Code = value[0]; }
    }
}

modelBuilder.Entity<Product>().Property(p => p.Code)
    .HasTypeName("char")
    .HasMaxLength(1)
Quid answered 13/3, 2019 at 13:27 Comment(1)
HasTypeName vs HasColumnType("char")Sealskin
M
0

There is alternate ways to tackle this issue for TESTING purpose only. Make the field not null to null able for time being from design mode. Sometime it is restricted SQL Management Studio. (Change setting Tools -> Option ->Designer -> Table Database designer -> Uncheck "Prevent saving changes that required table creation"

Menology answered 4/12, 2018 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.