0

When I write the code below with the PreditTotal argument, the resulting error message is:

The specified conversion (from the concrete type 'System.Decimal' to the nullable type 'System.Double') is invalid.

However, in the Model1.edmx file, it is defined as Nullable<double>. Why does it change to the Decimal type after execution?

code :

private IEnumerable<BalanceWarning.PaymentData> GetBalanceWarningData(string Year, string Month)
{
    using (var db = GetDB())
    {
        var result = db.V_VIPGetPaymentDataByDeptK
            .Where(s => s.Year == Year && s.Month == Month)
            .Select(t => new BalanceWarning.PaymentData
            {
                Year = t.Year,
                Month = t.Month,
                DeptId = t.DeptId,
                PreditPayment = t.PreditPayment,
                //PreditCost = (decimal)t.PreditCost,
                PreditTotal = (double?)t.PreditTotal
            })
            .ToList();
        return result;

    }
}

Model1.edmx view Content

public partial class V_VIPGetPaymentDataByDeptK
{
    public string Year { get; set; }
    public string Month { get; set; }
    public string DeptId { get; set; }
    public decimal PreditPayment { get; set; }
    public decimal RealPayment { get; set; }
    public double PreditCost { get; set; }
    public double RealCost { get; set; }
    public Nullable<double> PreditTotal { get; set; }
    public Nullable<double> RealTotal { get; set; }
}
class PaymentData

    public class PaymentData
    {
        public string Year { get; set; }
        public string Month { get; set; }
        public string DeptId { get; set; }
        public decimal PreditPayment { get; set; }
        public decimal PreditCost { get; set; }
        public double? PreditTotal { get; set; }
    }

View V_VIPGetPaymentDataByDeptK schema:

Column Name Data type
Year varchar
Month varchar
DeptId varchar
PreditPayment decimal
RealPayment decimal
PreditCost float
RealCost float
PreditTotal float
RealTotal float
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
yihang hwang
  • 419
  • 5
  • 15
  • Why do you have `(double?)t.PreditTotal` when `PreditTotal` is already ` double?`? – ProgrammingLlama Jun 13 '23 at 08:11
  • 2
    And what is the database type for the field? More specifically, my question is "does `var result = db.V_VIPGetPaymentDataByDeptK.ToList();` produce the same error?" – ProgrammingLlama Jun 13 '23 at 08:12
  • @ProgrammingLlama database that field type is float – yihang hwang Jun 13 '23 at 08:13
  • @ProgrammingLlama the error happen when V_VIPGetPaymentDataByDeptK select result into PaymentData – yihang hwang Jun 13 '23 at 08:15
  • Are you sure it's `PreditTotal` that is the issue? `PreditCost` is `double` in the first type and `decimal` in the second, although neither is nullable. In that first snippet, when you mouse over each of the `PreditTotal` occurrences, what type does Intellisense say they are? – jmcilhinney Jun 13 '23 at 08:15
  • Using different types for properties/columns of the same name is usually a sign that the model hasn't been well designed. – Damien_The_Unbeliever Jun 13 '23 at 08:17
  • @jmcilhinney PreditTotal intellisense both double? type , then compile will success but that internal server error when execute – yihang hwang Jun 13 '23 at 08:18
  • _"the error happen when V_VIPGetPaymentDataByDeptK select result into PaymentData"_ - that doesn't answer my question though. – ProgrammingLlama Jun 13 '23 at 08:21
  • @Damien_The_Unbeliever I am already delete that Model1.edmx V_VIPGetPaymentDataByDeptK Model then update the model again , but it still public Nullable type on PreditTotal arguements – yihang hwang Jun 13 '23 at 08:21
  • @ProgrammingLlama the code 1st version is PreditTotal = t.PreditTotal , but internal server error happen , it show that type is decimal (that 's problem) then i try (double ?) to solve this problem ,but stll not ok – yihang hwang Jun 13 '23 at 08:25
  • 2
    I think you're having trouble understanding what I'm asking. I'm asking you to go on a debugging mission. I'm asking you to see if `var result = db.V_VIPGetPaymentDataByDeptK.ToList();` exhibits the same behaviour as your problem query. Why do I want to know this? Well, the model for `V_VIPGetPaymentDataByDeptK` has the same type for `PreditTotal` as `PaymentData` does. My hunch is that EF isn't successfully converting from the database type to `decimal`, or perhaps `decimal?` in .NET. This should help to confirm it. I'm not asking because I don't understand your problem. We're on to diagnosis – ProgrammingLlama Jun 13 '23 at 08:29
  • 2
    Its still the question what database that is. The name V_something sounds like its a view and the field names are something that is calculated. Maybe you want to have a closer look at the Sql definition of that view how those fields are calculated and what datatypes are returned because of the way those fields are calculated. – Ralf Jun 13 '23 at 08:30
  • @ProgrammingLlama I try var result = db.V_VIPGetPaymentDataByDeptK.ToList(); and same error happen , i was wondering why no transfer to object will still error – yihang hwang Jun 13 '23 at 08:33
  • 3
    I have two dumb questions: what is the datatype of these two fields in the underlying database? Why are using using floats for money-related fields (https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency)? – Salman A Jun 13 '23 at 08:34
  • It seems quite likely that there's an incompatibility between what's actually being returned by the database and your model in C#. Is `V_VIPGetPaymentDataByDeptK` a view? If so, can you include the SQL used to generate that view, and perhaps also the schema for the underlying table? – ProgrammingLlama Jun 13 '23 at 08:39
  • @ProgrammingLlama its view , here 's schema below Column Name Data type Year varchar| Month varchar| DeptId varchar| PreditPayment decimal| RealPayment decimal| PreditCost float| RealCost float| PreditTotal float| RealTotal float| – yihang hwang Jun 13 '23 at 08:42
  • Also, EDMX might be out of sync, and EDMX cant be source of truth, depending on ORM you use – Code Name Jack Jun 13 '23 at 09:13
  • @ProgrammingLlama After deleting and re-creating the V_VIPGetPaymentDataByDeptK model, there were no modifications made and it still generated errors. What do you think would be the best way to fix this? – yihang hwang Jun 14 '23 at 01:41
  • @CodeNameJack how to solve that EDMX create wrong type of view column , I try to re-creating but still the same result – yihang hwang Jun 14 '23 at 03:19

0 Answers0