为什么我的LINQ查询结果与数据库SELECT查询结果不匹配?

时间:2018-06-27 12:54:33

标签: c# sql sql-server linq linq-to-sql

我已经确认我正在使用相同的服务器/数据库,但是,当我运行此查询时,SQL Server Profiler不会在跟踪中捕获任何东西。

public ClaimsViewModel GetSingleItem(int id)
{
    using (var db = new dbMedcoreDataContext())
    {
        var claim = db.Claims.Single(x => x.Id == id);
        return BuildViewModel(claim);
    }
}

示例记录如下:

enter image description here

因此,我的意思是明显地是存在引用和辅助DoctorId值,引用了Doctor#1。经过简单的选择,我在SSMS中看到了这一点:

SELECT * FROM Claims WHERE Id = 1

但是,上面的.Single()查询不会填充这些值。它们留在0。

数据库中没有结构化的关系。从理论上讲,这应该可以让我在测试期间更轻松地操作数据。

编辑3

每个注释,这是CREATE TABLE代码:

CREATE TABLE [dbo].[Claims](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ReferringDoctorId] [int] NOT NULL,
    [AssistingDoctorId] [int] NOT NULL,
    [AuthorizationCode] [varchar](50) NULL,
    [ServiceDateStart] [datetime] NOT NULL,
    [ServiceDateEnd] [datetime] NULL,
    [Height] [int] NULL,
    [Weight] [int] NULL,
    [InHospital] [bit] NULL,
    [fkPatientId] [int] NOT NULL,
    [Deleted] [bit] NOT NULL,
    [Submitted] [bit] NOT NULL,
 CONSTRAINT [PK_Claims] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Claims] ADD  CONSTRAINT [DF_Claims_Deleted]  DEFAULT ((0)) FOR [Deleted]
GO

ALTER TABLE [dbo].[Claims] ADD  CONSTRAINT [DF_Claims_Submitted]  DEFAULT ((0)) FOR [Submitted]
GO

编辑2

每个注释,这是即时窗口中?claim的结果。我在我认为的测试之间更改了一些值(因此,体重和身高可能有所不同),但Doctor Id值仍在执行相同的操作

?claim
{Ortund.Claim}
    AssistingDoctorId: 0
    AuthorizationCode: "1234"
    Deleted: false
    Height: 13
    Id: 1
    InHospital: false
    ReferringDoctorId: 0
    ServiceDateEnd: {2018-06-12 4:00:00 PM}
    ServiceDateStart: {2018-06-12 10:00:00 AM}
    Submitted: true
    Weight: 12
    _AssistingDoctorId: 0
    _AuthorizationCode: "1234"
    _Deleted: false
    _Height: 13
    _Id: 1
    _InHospital: false
    _ReferringDoctorId: 0
    _ServiceDateEnd: {2018-06-12 4:00:00 PM}
    _ServiceDateStart: {2018-06-12 10:00:00 AM}
    _Submitted: true
    _Weight: 12
    _fkPatientId: 1
    fkPatientId: 1

编辑

每个请求,这是Claim类(由dbml自动生成)

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Claims")]
public partial class Claim : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private int _ReferringDoctorId;

    private int _AssistingDoctorId;

    private string _AuthorizationCode;

    private System.DateTime _ServiceDateStart;

    private System.Nullable<System.DateTime> _ServiceDateEnd;

    private System.Nullable<int> _Height;

    private System.Nullable<int> _Weight;

    private System.Nullable<bool> _InHospital;

    private int _fkPatientId;

    private bool _Deleted;

    private bool _Submitted;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIdChanging(int value);
partial void OnIdChanged();
partial void OnReferringDoctorIdChanging(int value);
partial void OnReferringDoctorIdChanged();
partial void OnAssistingDoctorIdChanging(int value);
partial void OnAssistingDoctorIdChanged();
partial void OnAuthorizationCodeChanging(string value);
partial void OnAuthorizationCodeChanged();
partial void OnServiceDateStartChanging(System.DateTime value);
partial void OnServiceDateStartChanged();
partial void OnServiceDateEndChanging(System.Nullable<System.DateTime> value);
partial void OnServiceDateEndChanged();
partial void OnHeightChanging(System.Nullable<int> value);
partial void OnHeightChanged();
partial void OnWeightChanging(System.Nullable<int> value);
partial void OnWeightChanged();
partial void OnInHospitalChanging(System.Nullable<bool> value);
partial void OnInHospitalChanged();
partial void OnfkPatientIdChanging(int value);
partial void OnfkPatientIdChanged();
partial void OnDeletedChanging(bool value);
partial void OnDeletedChanged();
partial void OnSubmittedChanging(bool value);
partial void OnSubmittedChanged();
#endregion

    public Claim()
    {
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this.OnIdChanging(value);
                this.SendPropertyChanging();
                this._Id = value;
                this.SendPropertyChanged("Id");
                this.OnIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ReferringDoctorId", DbType="Int NOT NULL")]
    public int ReferringDoctorId
    {
        get
        {
            return this._ReferringDoctorId;
        }
        set
        {
            if ((this._ReferringDoctorId != value))
            {
                this.OnReferringDoctorIdChanging(value);
                this.SendPropertyChanging();
                this._ReferringDoctorId = value;
                this.SendPropertyChanged("ReferringDoctorId");
                this.OnReferringDoctorIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AssistingDoctorId", DbType="Int NOT NULL")]
    public int AssistingDoctorId
    {
        get
        {
            return this._AssistingDoctorId;
        }
        set
        {
            if ((this._AssistingDoctorId != value))
            {
                this.OnAssistingDoctorIdChanging(value);
                this.SendPropertyChanging();
                this._AssistingDoctorId = value;
                this.SendPropertyChanged("AssistingDoctorId");
                this.OnAssistingDoctorIdChanged();
            }
        }
    }

     [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AuthorizationCode", DbType="VarChar(50)")]
    public string AuthorizationCode
    {
        get
        {
            return this._AuthorizationCode;
        }
        set
        {
            if ((this._AuthorizationCode != value))
            {
                this.OnAuthorizationCodeChanging(value);
                this.SendPropertyChanging();
                this._AuthorizationCode = value;
                this.SendPropertyChanged("AuthorizationCode");
                this.OnAuthorizationCodeChanged();
            }
        }
    }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ServiceDateStart", DbType="DateTime NOT NULL")]
    public System.DateTime ServiceDateStart
    {
        get
        {
            return this._ServiceDateStart;
        }
        set
        {
            if ((this._ServiceDateStart != value))
            {
                this.OnServiceDateStartChanging(value);
                this.SendPropertyChanging();
                this._ServiceDateStart = value;
                this.SendPropertyChanged("ServiceDateStart");
                this.OnServiceDateStartChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ServiceDateEnd", DbType="DateTime")]
    public System.Nullable<System.DateTime> ServiceDateEnd
    {
        get
        {
            return this._ServiceDateEnd;
        }
        set
        {
            if ((this._ServiceDateEnd != value))
            {
                this.OnServiceDateEndChanging(value);
                this.SendPropertyChanging();
                this._ServiceDateEnd = value;
                this.SendPropertyChanged("ServiceDateEnd");
                this.OnServiceDateEndChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Height", DbType="Int")]
    public System.Nullable<int> Height
    {
        get
        {
            return this._Height;
        }
        set
        {
            if ((this._Height != value))
            {
                this.OnHeightChanging(value);
                this.SendPropertyChanging();
                this._Height = value;
                this.SendPropertyChanged("Height");
                this.OnHeightChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Weight", DbType="Int")]
    public System.Nullable<int> Weight
    {
        get
        {
            return this._Weight;
        }
        set
        {
            if ((this._Weight != value))
            {
                this.OnWeightChanging(value);
                this.SendPropertyChanging();
                this._Weight = value;
                this.SendPropertyChanged("Weight");
                this.OnWeightChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_InHospital", DbType="Bit")]
    public System.Nullable<bool> InHospital
    {
        get
        {
            return this._InHospital;
        }
        set
        {
            if ((this._InHospital != value))
            {
                this.OnInHospitalChanging(value);
                this.SendPropertyChanging();
                this._InHospital = value;
                this.SendPropertyChanged("InHospital");
                this.OnInHospitalChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_fkPatientId", DbType="Int NOT NULL")]
    public int fkPatientId
    {
        get
        {
            return this._fkPatientId;
        }
        set
        {
            if ((this._fkPatientId != value))
            {
                this.OnfkPatientIdChanging(value);
                this.SendPropertyChanging();
                this._fkPatientId = value;
                this.SendPropertyChanged("fkPatientId");
                this.OnfkPatientIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Deleted", DbType="Bit NOT NULL")]
    public bool Deleted
    {
        get
        {
            return this._Deleted;
        }
        set
        {
            if ((this._Deleted != value))
            {
                this.OnDeletedChanging(value);
                this.SendPropertyChanging();
                this._Deleted = value;
                this.SendPropertyChanged("Deleted");
                this.OnDeletedChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Submitted", DbType="Bit NOT NULL")]
    public bool Submitted
    {
        get
        {
            return this._Submitted;
        }
        set
        {
            if ((this._Submitted != value))
            {
                this.OnSubmittedChanging(value);
                this.SendPropertyChanging();
                this._Submitted = value;
                this.SendPropertyChanged("Submitted");
                this.OnSubmittedChanged();
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

结果是我没有使用问题中未包含的代码将属性值映射到Viewmodel。