How to get rid of 'property could not be set t

2019-05-14 08:37发布

问题:

I'm trying to get a function import to work correctly. EF calls out to my stored procedure, but the result has an inner exception that I don't understand:

var result = context.SomeFunctionImport();

I get:

The 'Cnt' property on 'SomeClass' could not be set to a 'Double' value. You must set this property to a non-null value of type 'Decimal'.

Here's the Cnt property on SomeClass:

    [DataMember]
    public Nullable<decimal> Cnt
    {
        get { return _cnt; }
        set
        {
            if (_cnt != value)
            {
                OnComplexPropertyChanging();
                _cnt = value;
                OnPropertyChanged("Cnt");
            }
        }
    }
    private Nullable<decimal> _cnt;

回答1:

You need to define it like this: public decimal Cnt



回答2:

Found the issue. My stored procedure was missing a cast on Cnt after a rounding operation.



回答3:

I think this field is float type in your database table. so you need to set it double? if this field is nullable or otherwise use double

public double? field_name {get; set}
// or 
public double field_name {get; set}

It's working for me



回答4:

I had this problem and my issue was that on the DB, the type was real and in the EF class was declared as decimal. I just changed the DB type to real and problem solved.