也许有人可以提供帮助。
我希望映射Linq-Class不同的数据类型。
这是有效的:
private System.Nullable<short> _deleted = 1;
[Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)]
public System.Nullable<short> deleted
{
get
{
return this._deleted;
}
set
{
this._deleted = value;
}
}
当然可以。但是,当我想为布尔值放置一些逻辑时,不会这样:
private System.Nullable<short> _deleted = 1;
[Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)]
public bool deleted
{
get
{
if (this._deleted == 1)
{
return true;
}
return false;
}
set
{
if(value == true)
{
this._deleted = (short)1;
}else
{
this._deleted = (short)0;
}
}
}
我总是遇到运行时错误:
[TypeLoadException: GenericArguments[2], "System.Nullable`1[System.Int16]", on 'System.Data.Linq.Mapping.PropertyAccessor+Accessor`3[T,V,V2]' violates the constraint of type parameter 'V2'.]
我无法将数据库更改为位...我需要在映射类中进行转换。
** 更新
根据mmcteam的说法,使用未映射的方法进行转换就可以了。 不太确定是否以这种方式使用OR Mapping,但它有效。
private System.Nullable<short> _deleted = 1;
public bool deleted
{
get
{
if (this._deleted == 0)
{
return false;
}
else
{
return true;
}
}
set
{
if (value)
{
this._deleted = 1;
}
else
{
this._deleted = 0;
}
}
}
[Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)]
private short? deleted_smallint
{
get
{
return this._deleted;
}
set
{
this._deleted = value;
}
}
** 通知/问题
您不能在linq查询中使用未映射的方法!
var result = from p in dc.Products
where p.enabled && !p.deleted
select p;
导致不支持sql异常。如果没有where条件,数据将以正确的值显示出来。
答案 0 :(得分:4)
或者只是在您的行类中再添加一个属性,然后将前一个属性转换为bool。
答案 1 :(得分:0)
你不想要这个:
[Column(Storage = "_deleted", Name = "deleted", DbType = "Bit", CanBeNull = false)]
public bool deleted ...
而不是:
[Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)]
public bool deleted ...