Linq-to-Sql mapping / casting issue for different types

maybe someone can help.

I want to have a matched Linq-Class data type.

It works:

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

Of course. But no, when I want to put logic for a logical one, for example:

  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 always get a 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 not change the database to bits. I need to have casting in a mapping class.

** Update

According to mmcteam, casting in an unclaimed method does the trick. Not sure if you are going to use OR matching this 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 cannot use methods that are not mapped to linq queries!

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

throws an invalid sql exception. Without the where clause, the data is output with the correct values.

+4
source share
2 answers

Or just add another property to your string class and drop the previous one on bool.

+4
source

You do not 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 ... 

?

0
source

Source: https://habr.com/ru/post/1308649/


All Articles