Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are valid primitive properties in Entity Framework Code First?

When I try to map a column to a char data type in my model class I get an error:

The property '[ColumnName]' is not a declared property on type '[ClassName]'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

What are the valid primitive types for EF Code First?

like image 496
Ryan Avatar asked Jul 08 '11 18:07

Ryan


1 Answers

This is interesting but you really cannot map char property. I just checked it and if you want to have char(1) in the database you must use string property with following mapping:

modelBuilder.Entity<MyEntity>()
            .Property(p => p.MyProperty)
            .HasMaxLength(1)
            .IsFixedLength()
            .IsUnicode(false);

It is not only problem of Code-first. It is whole EF limitation because EDMX designer also doesn't show char type. I think allowed types will be same as described in CSDL reference for EDMX because code first is just wrapper around the same mapping infrastructure.

like image 62
Ladislav Mrnka Avatar answered Sep 28 '22 08:09

Ladislav Mrnka