实体框架code一流的布尔和列的整数之间的转换

编程入门 行业动态 更新时间:2024-10-24 06:28:47
本文介绍了实体框架code一流的布尔和列的整数之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用实体框架5 code首先。我的表中有一个名为列活动和其数据类型为类型 INT 。存储在Active的值 0 , 1 和空。

我有我需要映射到该表中的一类。

公共类CommandExecutionServer:IEntity {      公众诠释编号{获得;组; }      公共BOOL? IsActive {获得;组; } }

下面是我的配置文件。我想在我的课地图我布尔属性的整型字段在数据库中。

类CommandExecutionServerConfiguration:EntityTypeConfiguration< CommandExecutionServer> {      内部CommandExecutionServerConfiguration()      {           this.ToTable(tblCommandExecutionServers);           this.Property(X => x.IsActive).HasColumnName(激活)HasColumnType(位)。      } }

这都不尽如人意。那我得到的错误是:

在CommandExecutionServer的IsActive属性不能被设置为的Int32的价值。您必须将此属性设置为类型的非空值布尔

我尝试添加 .HasColumnType(位),认为它可能需要我的问题。我该怎么做呢?理想情况下,我想0为假,1为真,空为空,而任何其他数字为false。

更新

如果我上面的改变:

this.Property(X => x.IsActive).HasColumnName(激活)HasColumnType(INT)。

...然后我得到了以下错误:

指定

会员映射无效。类型Edm.Boolean [可空=真,默认值='类型成员IsActive的MyProject.Infrastructure.EntityFramework.CommandExecutionServer'是不符合SqlServer.int [可空=真,默认值='成员的兼容有效类型codeFirstDatabaseSchema.CommandExecutionServer。

解决方案

我尝试以下,因为我不知道,如果实体框架能够处理的转换对我来说。

我删除了这一行:

this.Property(X => x.IsActive).HasColumnName(激活)HasColumnType(INT)。

我加到一个属性来我 CommandExecutionServer类:

公共类CommandExecutionServer:IEntity {      公众诠释编号{获得;组; }      公众诠释?活动{获得;组; }      公共布尔IsActive      {           得到           {                (主动== 1)?真假;           }      } }

有可能是一个更好的办法,但这对我的作品现在。如果任何一个可以更好的这个话,请继续:)

I am using Entity Framework 5 code first. My table has a column called Active and its datatype is of type int. The values that are stored in Active are 0, 1 and null.

I have a class that I need to map to this table.

public class CommandExecutionServer : IEntity { public int Id { get; set; } public bool? IsActive { get; set; } }

Here is my configuration file. I am trying to map my boolean property in my class to the integer field in the database.

class CommandExecutionServerConfiguration : EntityTypeConfiguration<CommandExecutionServer> { internal CommandExecutionServerConfiguration() { this.ToTable("tblCommandExecutionServers"); this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("bit"); } }

This is not working well. The error that I am getting is:

The 'IsActive' property on 'CommandExecutionServer' could not be set to a 'Int32' value. You must set this property to a non-null value of type 'Boolean'

I tried adding .HasColumnType("bit") and thought that it might take of my problem. How do I do this? Ideally I would like 0 to be false, 1 to true, null to be null, and any other number to false.

UPDATE

If I change the above to:

this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("int");

...then I get the following error:

Member Mapping specified is not valid. The type 'Edm.Boolean[Nullable=True,DefaultValue=]' of member 'IsActive' in type 'MyProject.Infrastructure.EntityFramework.CommandExecutionServer' is not compatible with 'SqlServer.int[Nullable=True,DefaultValue=]' of member 'Active' in type 'CodeFirstDatabaseSchema.CommandExecutionServer'.

解决方案

I tried the following because I do not know if Entity Framework can handle the conversion for me.

I removed this line:

this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("int");

I then added a property to my CommandExecutionServer class:

public class CommandExecutionServer : IEntity { public int Id { get; set; } public int? Active { get; set; } public bool IsActive { get { return (Active == 1) ? true : false; } } }

There might be a better way but this works for me for now. If any one can better this then please go ahead :)

更多推荐

实体框架code一流的布尔和列的整数之间的转换

本文发布于:2023-11-06 22:40:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   整数   实体   框架   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!