views:

235

answers:

2

Hi, maybe someone can help.

I want to have on mapped Linq-Class different Datatype.

This is working:

 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;
        }
    }

Sure thing. But no when i want to place some logic for boolean, like this:

 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;
    }
        }
    }

I get always runtime error:

[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'.]

I can't change the database to bit.. I need to have casting in mapping class.

** Update

According to mmcteam, casting in unmapped method does the trick. Not so sure if it is ment to be using the OR Mapping that way, but it works.

    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;
        }
    }

** NOTICE/PROBLEM

You can't use the not mapped methods on linq queries!

                 var result = from p in dc.Products
                               where p.enabled && !p.deleted 
                select p;

causes not supported sql exception. Without the where condition, data comes out with correct values.

A: 

Don't you want this:

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

Instead of this:

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

?

Daniel Renshaw
No, because this it not working too. dbtype is not Boleanen, is unchangeable SmallInt! This causes just another runtime error of wrong datatype Boolean.
csharpnoob
+3  A: 

Or just add one more property to your row class and cast previous one to bool.

mmcteam.com.ua
your right! this did it.
csharpnoob
okay, we just reached the next problem. methods which are not mapped can't be used in linq query. see update in question.
csharpnoob
It is bad idea to use custom properties in your linq query, imagine that you want to group or order by that new property. Linq can't generate that query for you. But depending on situation you can write all you need on linq, then execute query(for example calling ToList()) and then working with in-memory objects you can use any cutom methods in your linqToObjects query
mmcteam.com.ua
yes but then then casting methods doesnt solve my problem in the first place. ToList() isn't a proper solution, cause x-times more sql selects and memory/performance issue.
csharpnoob
I understand, it depends on usage. As I can see the only way for you - edit db structure(or in all your queries make same cast as in your property, but not all casts are allowed by EF)
mmcteam.com.ua
You can consider using Materialize<> from EFExtensions to build custom sql but this is last thing I would try to do.
mmcteam.com.ua