Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何使用Entity Framework 4.1“映射char属性;“仅限代码”;流畅的API?_Entity Framework_Char_Ef4 Code Only_Fluent Interface - Fatal编程技术网

Entity framework 如何使用Entity Framework 4.1“映射char属性;“仅限代码”;流畅的API?

Entity framework 如何使用Entity Framework 4.1“映射char属性;“仅限代码”;流畅的API?,entity-framework,char,ef4-code-only,fluent-interface,Entity Framework,Char,Ef4 Code Only,Fluent Interface,我有一个具有char属性的对象: public class Product { public char Code { get; set; } } 实体框架似乎无法映射字符(当我从模型对象创建数据库架构时,数据库中缺少此字段)。我是否可以使用fluent API映射字符(如字符串)?我不想更改模型对象,因为它们是遗留共享库的一部分 Char不是实体框架的有效基元类型=实体框架未映射它。如果选中,您将看到有效类型的列表(char不在其中) 数

我有一个具有char属性的对象:

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

实体框架似乎无法映射字符(当我从模型对象创建数据库架构时,数据库中缺少此字段)。我是否可以使用fluent API映射字符(如字符串)?我不想更改模型对象,因为它们是遗留共享库的一部分

Char
不是实体框架的有效基元类型=实体框架未映射它。如果选中,您将看到有效类型的列表(
char
不在其中)

数据库
char(1)
被翻译为
string
()<代码>字符描述为


唯一难看的选项是使用字符串的第二个映射属性,而您的
char
非映射属性将只使用该属性中的
string[0]
。这只是EF中缺少一些简单类型映射或转换器的另一个示例。

在Fluent API中,您可以使用HasColumnType方法指定数据库列数据类型,如下所示:

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

有其他方法解决此问题,仅用于测试目的。使字段从设计模式暂时不为null变为null。有时它是受限的SQL管理工作室。(更改设置工具->选项->设计器->表数据库设计器->取消选中“防止保存需要创建表的更改”

我已经尝试了我想象的所有方法,我必须说,据我所知,接受的答案是解决字符类型问题的唯一方法

char类型在EntityFramework中不可用

Fluent API包含在该限制中

如果您试图在
属性(p=>p.MyCharProperty)上放置字符,则
将给您一个异常

这意味着char属性不可用于Fluent API或属性

最简单的解决方案就是这个(由Ladislav Mrnka提出)

注意:不能将属性设置为私有、受保护或内部。必须是公共的

Fluent API版本将是这样的

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().Property(p=>p.code)
.HasTypeName(“char”)
.HasMaxLength(1)
[列(TypeName=“char(1)”)]


对于我来说,EF core 3.1.4是有效的,如果你想要一个单独的
char
,那么添加属性
[StringLength(1,MinimumLength=1)]
[StringLength(1)]
对于EF 6.1.3中的
varchar(1)
就足够了:另一个fluentApi选项:
modelBuilder.Entity().Property(p=>p.code)。HasMaxLength(你想要的数字).IsFixedLength();
这将生成char(您想要的数字)
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]; }
    }
}
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)