在实体框架中将模型属性设置为布尔值 [英] Set model property to boolean in Entity Framework
问题描述
我刚刚开始学习一些有关实体框架的知识,对ORM的了解也很少。
I am just starting to learn a bit about the entity framework and don't have much experience with ORM's.
在我的小应用程序中,我有一张桌子, sql server表有几列,包括PrimaryKey(int),Name(string)和Flag(tinyint)。
In my little app I have one table, this sql server table has several columns including a PrimaryKey (int) a Name (string) and a Flag (tinyint).
当我将此表导入到其中时,会自动为其分配Flags '数据类型为字节。很好,但是Flag确实应该是布尔值,所以我
When I imported this table into it automatically assigned the Flags' datatype as a byte. This is fine, but the Flag should really be a boolean, so I
- 单击映射详细信息
- 选择我的Flag属性
- 将类型从字节更改为布尔值
- 重建应用程序
- Clicked on the Mapping Details
- Selected my Flag property
- Changed the Type from Byte to Boolean
- Rebuilt the application
然后出现此错误:
错误2019:指定了成员映射
无效。
'MyModel.MyItem'类型的成员'MyFlag'的
类型'Edm.Boolean [Nullable = True,DefaultValue =]'
与$ b $不兼容b'SqlServer.tinyint [Nullable = True,DefaultValue =]'
成员'MyFlag',类型为
'MyModel.Store.MyItem'。
Error 2019: Member Mapping specified is not valid. The type 'Edm.Boolean[Nullable=True,DefaultValue=]' of member 'MyFlag' in type 'MyModel.MyItem' is not compatible with 'SqlServer.tinyint[Nullable=True,DefaultValue=]' of member 'MyFlag' in type 'MyModel.Store.MyItem'.
有没有办法
MyItem item = new MyItem();
item.Flag = true;
并在数据库中将Flag保存为1?
and have Flag save to 1 in the database?
推荐答案
您可以将 MyFlag
的数据类型更改为 bit
。
You could change the datatype of MyFlag
to bit
in the database.
这篇关于在实体框架中将模型属性设置为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!