简体   繁体   中英

SQL query doesn't return the same result in SQL Server and in C#

I launched a query in two different environments:

  • Microsoft SQL Server Management Studio
  • C# (.Net Core 2.2.1)

The query:

SELECT
    CASE 
       WHEN afc.Id IS NULL 
          THEN fcv.Id 
          ELSE afc.ParentTemplateFieldContainerId 
    END AS FieldContainerId,
    fcv.TemplateFieldContainerId,
    CASE 
       WHEN fcvp.ProductId IS NULL 
          THEN '00000000-0000-0000-0000-000000000000' 
          ELSE fcvp.ProductId 
    END AS ProductId
FROM 
    FieldContainerValue fcv
LEFT JOIN
    FieldContainerValueAndProduct fcvp ON fcvp.FieldContainerValueId = fcv.Id
LEFT JOIN
    ArrayFieldContainer afc ON afc.ChildTemplateFieldContainer = fcv.Id
WHERE 
    fcv.Id IN ('337c0b2e-a83b-4014-a46a-6ce8bf9fd2c4', 'c2ca899c-28ac-4dc3-bba9-f0741bb02097')

The code I used to run the query in C#:

public IEnumerable<T> QuerySql<T> (string sql)
{
    using (var conn = new SqlConnection(ConfigMan.GetConfigProperty("ConnectionString")))
    {
        conn.Open();
        conn.EnlistTransaction(Transaction.Current);
        return conn.Query<T>(sql);
    }
}

SQL is the query.

The type that my query returns:

public class FieldContainerValueProperties
{
    public Guid FieldContainerValueId { get; set; }
    public Guid TemplateFieldContainerId { get; set; }
    public Guid ProductId { get; set; }
}

Results of the query: in both cases, I get the same number of items (2), but the FieldContainerId differs.

In SQL Server Management Studio:

[
    {
        "FieldContainerId": "337C0B2E-A83B-4014-A46A-6CE8BF9FD2C4",
        "TemplateFieldContainerId": "B4566675-B547-4025-ABBB-F5CC523BB092",
        "ProductId": "6E790000-FF4B-0003-C22D-08D68AB637D6"
    },
    {
        "FieldContainerId": "C2CA899C-28AC-4DC3-BBA9-F0741BB02097",
        "TemplateFieldContainerId": "591D8371-EDE8-460C-9847-3F2963D875D2",
        "ProductId": "6E790000-FF4B-0003-C22D-08D68AB637D6"
    }
]

From C#:

[
    {
        "FieldContainerId": "00000000-0000-0000-0000-000000000000",
        "TemplateFieldContainerId": "B4566675-B547-4025-ABBB-F5CC523BB092",
        "ProductId": "6E790000-FF4B-0003-C22D-08D68AB637D6"
    },
    {
        "FieldContainerId": "00000000-0000-0000-0000-000000000000",
        "TemplateFieldContainerId": "591D8371-EDE8-460C-9847-3F2963D875D2",
        "ProductId": "6E790000-FF4B-0003-C22D-08D68AB637D6"
    }
]

Possible explanations?

One possible explanation I can imagine is that in one case is null returned true and in the other case false , but I'm really not sure.

If that is indeed the case, do you know what could cause this problem ?

Your mapping is not working. Your model class contains FieldContainerValueId and your sql table FieldContainerId so when it maps field names are different and set value to default. Change your Model class FieldContainerValueId -> FieldContainerId . New model that will work fine,

public class FieldContainerValueProperties
{
    public Guid FieldContainerId { get; set; }
    public Guid TemplateFieldContainerId { get; set; }
    public Guid ProductId { get; set; }
}

**I have answered this in comments section. Writing here to just to get the post a resolved answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM