【问题标题】:Problem with Mapping/Casting Linq-to-Sql on different Types在不同类型上映射/转换 Linq-to-Sql 的问题
【发布时间】:2010-05-03 22:09:56
【问题描述】:

也许有人可以帮忙。

我想在映射的 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'.]

我无法将数据库更改为 bit.. 我需要在映射类中进行强制转换。

** 更新

根据 mmcteam 的说法,以未映射的方法进行投射可以解决问题。 不太确定是否以这种方式使用 OR 映射,但它确实有效。

    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 条件,数据就会得到正确的值。

【问题讨论】:

    标签: c# linq linq-to-sql casting mapping


    【解决方案1】:

    或者只是向您的行类添加一个属性并将前一个属性转换为布尔值。

    【讨论】:

    • 好的,我们刚刚遇到了下一个问题。未映射的方法不能用于 linq 查询。查看相关更新。
    • 在您的 linq 查询中使用自定义属性是个坏主意,假设您想按该新属性进行分组或排序。 Linq 无法为您生成该查询。但根据情况,您可以在 linq 上编写所需的所有内容,然后执行查询(例如调用 ToList()),然后使用内存中的对象,您可以在 linqToObjects 查询中使用任何自定义方法
    • 是的,但是然后转换方法首先不能解决我的问题。 ToList() 不是一个合适的解决方案,会导致多倍的 sql 选择和内存/性能问题。
    • 我明白了,这取决于使用情况。正如我所看到的那样,唯一的方法是编辑数据库结构(或在所有查询中进行与您的属性相同的转换,但并非所有转换都被 EF 允许)
    • 您可以考虑使用 EFExtensions 中的 Materialize 来构建自定义 sql,但这是我最不想做的事情。
    【解决方案2】:

    你不想这样吗:

    [Column(Storage = "_deleted", Name = "deleted", DbType = "Bit", CanBeNull = false)] 
    public bool deleted ...
    

    而不是这个:

    [Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)] 
    public bool deleted ...
    

    ?

    【讨论】:

    • 不,因为它也不起作用。 dbtype 不是 Boleanen,是不可更改的 SmallInt!这只会导致另一个错误数据类型布尔的运行时错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2020-07-12
    • 2010-11-26
    • 2012-01-02
    相关资源
    最近更新 更多