Nullable 和 Int32 类型之间未定义 Equal

2023-11-30

我正在编写一个无聊的应用程序来管理患者及其临床病史。我将 SQLite 与 DbLinq 库和 DbMetal 代码生成实用程序结合使用。以下是从底层数据库中提取的生成代码中的两个类:

[Table(Name="main.Patients")]
public partial class Patient : System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
{

    private static System.ComponentModel.PropertyChangingEventArgs emptyChangingEventArgs = new System.ComponentModel.PropertyChangingEventArgs("");

    private long _birthday;

    private string _firstName;

    private int _hasChildren;

    private System.Nullable<int> _id;

    private int _isMarried;

    private string _lastName;

    private string _profession;

    private EntitySet<ClinicCase> _clinicCases;

    private EntitySet<PatientAddress> _patientsAddresses;

    private EntitySet<PatientPhoneNumber> _patientsPhoneNumbers;

    #region Extensibility Method Declarations
    partial void OnCreated();

    partial void OnBirthdayChanged();

    partial void OnBirthdayChanging(long value);

    partial void OnFirstNameChanged();

    partial void OnFirstNameChanging(string value);

    partial void OnHasChildrenChanged();

    partial void OnHasChildrenChanging(int value);

    partial void OnIDChanged();

    partial void OnIDChanging(System.Nullable<int> value);

    partial void OnIsMarriedChanged();

    partial void OnIsMarriedChanging(int value);

    partial void OnLastNameChanged();

    partial void OnLastNameChanging(string value);

    partial void OnProfessionChanged();

    partial void OnProfessionChanging(string value);
    #endregion


    public Patient()
    {
        _clinicCases = new EntitySet<ClinicCase>(new Action<ClinicCase>(this.ClinicCases_Attach), new Action<ClinicCase>(this.ClinicCases_Detach));
        _patientsAddresses = new EntitySet<PatientAddress>(new Action<PatientAddress>(this.PatientsAddresses_Attach), new Action<PatientAddress>(this.PatientsAddresses_Detach));
        _patientsPhoneNumbers = new EntitySet<PatientPhoneNumber>(new Action<PatientPhoneNumber>(this.PatientsPhoneNumbers_Attach), new Action<PatientPhoneNumber>(this.PatientsPhoneNumbers_Detach));
        this.OnCreated();
    }

    [Column(Storage="_birthday", Name="Birthday", DbType="integer", AutoSync=AutoSync.Never, CanBeNull=false)]
    [DebuggerNonUserCode()]
    public long BirthdayBinaryDate
    {
        get
        {
            return this._birthday;
        }
        set
        {
            if ((_birthday != value))
            {
                this.OnBirthdayChanging(value);
                this.SendPropertyChanging();
                this._birthday = value;
                this.SendPropertyChanged("Birthday");
                this.OnBirthdayChanged();
            }
        }
    }

    [Column(Storage="_firstName", Name="FirstName", DbType="text", AutoSync=AutoSync.Never, CanBeNull=false)]
    [DebuggerNonUserCode()]
    public string FirstName
    {
        get
        {
            return this._firstName;
        }
        set
        {
            if (((_firstName == value) 
                        == false))
            {
                this.OnFirstNameChanging(value);
                this.SendPropertyChanging();
                this._firstName = value;
                this.SendPropertyChanged("FirstName");
                this.OnFirstNameChanged();
            }
        }
    }

    [Column(Storage="_hasChildren", Name="HasChildren", DbType="integer", AutoSync=AutoSync.Never, CanBeNull=false)]
    [DebuggerNonUserCode()]
    public int HasChildren
    {
        get
        {
            return this._hasChildren;
        }
        set
        {
            if ((_hasChildren != value))
            {
                this.OnHasChildrenChanging(value);
                this.SendPropertyChanging();
                this._hasChildren = value;
                this.SendPropertyChanged("HasChildren");
                this.OnHasChildrenChanged();
            }
        }
    }

    [Column(Storage="_id", Name="ID", DbType="integer", IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    [DebuggerNonUserCode()]
    public System.Nullable<int> ID
    {
        get
        {
            return this._id;
        }
        set
        {
            if ((_id != value))
            {
                this.OnIDChanging(value);
                this.SendPropertyChanging();
                this._id = value;
                this.SendPropertyChanged("ID");
                this.OnIDChanged();
            }
        }
    }

    [Column(Storage="_isMarried", Name="IsMarried", DbType="integer", AutoSync=AutoSync.Never, CanBeNull=false)]
    [DebuggerNonUserCode()]
    public int IsMarried
    {
        get
        {
            return this._isMarried;
        }
        set
        {
            if ((_isMarried != value))
            {
                this.OnIsMarriedChanging(value);
                this.SendPropertyChanging();
                this._isMarried = value;
                this.SendPropertyChanged("IsMarried");
                this.OnIsMarriedChanged();
            }
        }
    }

    [Column(Storage="_lastName", Name="LastName", DbType="text", AutoSync=AutoSync.Never, CanBeNull=false)]
    [DebuggerNonUserCode()]
    public string LastName
    {
        get
        {
            return this._lastName;
        }
        set
        {
            if (((_lastName == value) 
                        == false))
            {
                this.OnLastNameChanging(value);
                this.SendPropertyChanging();
                this._lastName = value;
                this.SendPropertyChanged("LastName");
                this.OnLastNameChanged();
            }
        }
    }

    [Column(Storage="_profession", Name="Profession", DbType="text", AutoSync=AutoSync.Never)]
    [DebuggerNonUserCode()]
    public string Profession
    {
        get
        {
            return this._profession;
        }
        set
        {
            if (((_profession == value) 
                        == false))
            {
                this.OnProfessionChanging(value);
                this.SendPropertyChanging();
                this._profession = value;
                this.SendPropertyChanged("Profession");
                this.OnProfessionChanged();
            }
        }
    }

    #region Children
    [Association(Storage="_clinicCases", OtherKey="PatientID", ThisKey="ID", Name="fk_ClinicCases_0")]
    [DebuggerNonUserCode()]
    public EntitySet<ClinicCase> ClinicCases
    {
        get
        {
            return this._clinicCases;
        }
        set
        {
            this._clinicCases = value;
        }
    }

    [Association(Storage="_patientsAddresses", OtherKey="PatientID", ThisKey="ID", Name="fk_PatientsAddresses_0")]
    [DebuggerNonUserCode()]
    public EntitySet<PatientAddress> Addresses
    {
        get
        {
            return this._patientsAddresses;
        }
        set
        {
            this._patientsAddresses = value;
        }
    }

    [Association(Storage="_patientsPhoneNumbers", OtherKey="PatientID", ThisKey="ID", Name="fk_PatientsPhoneNumbers_0")]
    [DebuggerNonUserCode()]
    public EntitySet<PatientPhoneNumber> PhoneNumbers
    {
        get
        {
            return this._patientsPhoneNumbers;
        }
        set
        {
            this._patientsPhoneNumbers = value;
        }
    }
    #endregion

    public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

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

    protected virtual void SendPropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler h = this.PropertyChanged;
        if ((h != null))
        {
            h(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }

    #region Attachment handlers
    private void ClinicCases_Attach(ClinicCase entity)
    {
        this.SendPropertyChanging();
        entity.Patient = this;
    }

    private void ClinicCases_Detach(ClinicCase entity)
    {
        this.SendPropertyChanging();
        entity.Patient = null;
    }

    private void PatientsAddresses_Attach(PatientAddress entity)
    {
        this.SendPropertyChanging();
        entity.Patient = this;
    }

    private void PatientsAddresses_Detach(PatientAddress entity)
    {
        this.SendPropertyChanging();
        entity.Patient = null;
    }

    private void PatientsPhoneNumbers_Attach(PatientPhoneNumber entity)
    {
        this.SendPropertyChanging();
        entity.Patient = this;
    }

    private void PatientsPhoneNumbers_Detach(PatientPhoneNumber entity)
    {
        this.SendPropertyChanging();
        entity.Patient = null;
    }
    #endregion
}

[Table(Name="main.PatientsAddresses")]
public partial class PatientAddress : System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
{

    private static System.ComponentModel.PropertyChangingEventArgs emptyChangingEventArgs = new System.ComponentModel.PropertyChangingEventArgs("");

    private string _address;

    private string _domicileStatus;

    private System.Nullable<int> _patientID;

    private EntityRef<Patient> _patients = new EntityRef<Patient>();

    #region Extensibility Method Declarations
    partial void OnCreated();

    partial void OnAddressChanged();

    partial void OnAddressChanging(string value);

    partial void OnDomicileStatusChanged();

    partial void OnDomicileStatusChanging(string value);

    partial void OnPatientIDChanged();

    partial void OnPatientIDChanging(System.Nullable<int> value);
    #endregion


    public PatientAddress()
    {
        this.OnCreated();
    }

    [Column(Storage="_address", Name="Address", DbType="text", IsPrimaryKey=true, AutoSync=AutoSync.Never)]
    [DebuggerNonUserCode()]
    public string Address
    {
        get
        {
            return this._address;
        }
        set
        {
            if (((_address == value) 
                        == false))
            {
                this.OnAddressChanging(value);
                this.SendPropertyChanging();
                this._address = value;
                this.SendPropertyChanged("Address");
                this.OnAddressChanged();
            }
        }
    }

    [Column(Storage="_domicileStatus", Name="DomicileStatus", DbType="text", AutoSync=AutoSync.Never)]
    [DebuggerNonUserCode()]
    public string DomicileStatus
    {
        get
        {
            return this._domicileStatus;
        }
        set
        {
            if (((_domicileStatus == value) 
                        == false))
            {
                this.OnDomicileStatusChanging(value);
                this.SendPropertyChanging();
                this._domicileStatus = value;
                this.SendPropertyChanged("DomicileStatus");
                this.OnDomicileStatusChanged();
            }
        }
    }

    [Column(Storage="_patientID", Name="PatientID", DbType="integer", IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.Never)]
    [DebuggerNonUserCode()]
    public System.Nullable<int> PatientID
    {
        get
        {
            return this._patientID;
        }
        set
        {
            if ((_patientID != value))
            {
                if (_patients.HasLoadedOrAssignedValue)
                {
                    throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                }
                this.OnPatientIDChanging(value);
                this.SendPropertyChanging();
                this._patientID = value;
                this.SendPropertyChanged("PatientID");
                this.OnPatientIDChanged();
            }
        }
    }

    #region Parents
    [Association(Storage="_patients", OtherKey="ID", ThisKey="PatientID", Name="fk_PatientsAddresses_0", IsForeignKey=true)]
    [DebuggerNonUserCode()]
    public Patient Patient
    {
        get
        {
            return this._patients.Entity;
        }
        set
        {
            if (((this._patients.Entity == value) 
                        == false))
            {
                if ((this._patients.Entity != null))
                {
                    Patient previousPatients = this._patients.Entity;
                    this._patients.Entity = null;
                    previousPatients.Addresses.Remove(this);
                }
                this._patients.Entity = value;
                if ((value != null))
                {
                    value.Addresses.Add(this);
                    _patientID = value.ID;
                }
                else
                {
                    _patientID = null;
                }
            }
        }
    }
    #endregion

    public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

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

    protected virtual void SendPropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler h = this.PropertyChanged;
        if ((h != null))
        {
            h(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

我使用以下代码向患者添加地址:

PatientAddress address = new PatientAddress();
address.Address = txtAddress.Text;
address.DomicileStatus = cmbDomicileStatus.Text;
currentPatient.Addresses.Add(address);

Database.Source.PatientsAddresses.InsertOnSubmit(address);
Database.Source.SubmitChanges();

Database.Source 是在生成的代码中扩展 DataContext 的类的实例。在 SubmitChanges 上,我收到此异常:

“Nullable(Of Int32) 和 Int32 之间未定义等于运算符。”

消息不是一字不差地转述,但意思是一样的。堆栈跟踪指向 DbLinq 代码,更准确地说,指向源文件 DbLinq.Data.Linq.DataContext.cs 的第 709 行。您可以在这里找到源文件:http://dblinq.codeplex.com/SourceControl/changeset/view/16800#314775(在方法 SetEntityRefQueries(objectEntity) 的主体下)。 我发现在将外键值与表达式树中的常量进行比较时出现问题,但我无法获取有关该问题的其他信息。你能帮我找出问题所在吗?

注意:在调用 SubmitChanges 之前,字段 address.PatientID(外键)实际上已设置为正确的值。


正如我在上面的评论中提到的(我在这里重复,以便我可以链接图像),您的主键不应该为空。你的映射中应该有一个属性,你可以更改它来设置它,尽管我不使用 DbLinq,所以我不能直接给你它的屏幕截图。相反,它位于 LINQ-2-SQL DBML 设计器(左)和实体框架 EDMX 设计器(右)中。

enter image description here enter image description here


我不太确定你的删除问题 - 这似乎对我有用。您可以编辑您的问题以包含整个删除代码块吗?作为初步猜测,您要么创建一个新对象(而不是加载对象),然后尝试删除它,要么删除关联而不删除该对象。

作为一般规则,当我可以避免时,我绝不会从数据库中删除 - 我只是将其标记为非活动状态。这样“取消删除”要容易得多。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Nullable 和 Int32 类型之间未定义 Equal 的相关文章

随机推荐

  • 我可以将文件添加到本地 Git 存储库但不将其推送到远程吗

    我有许多大型视频文件 我不想将其推送到远程 但我确实想将它们添加到本地存储库 有点像远程版本 gitignore file 那可能吗 更具体地说 我正在开发一个多媒体项目 一个文件夹包含项目元数据文件 它是一个 Screenflow 项目
  • ggplot 中的反向日期时间(POSIXct 数据)轴

    我正在尝试使用 ggplot 创建 POSIXct 时间图 并且想反转轴 但正在努力使其工作 我一直在使用scale y datetime 因为在我的实际应用程序中 控制该轴上的中断非常重要 这是我的问题的一个例子 首先是正常排序 然后是我
  • 获取 Android 设备的永久唯一 ID

    我有一个应用程序 我想在其中使用唯一 ID 来识别设备 我尝试了多种解决方案 但在手机重置后唯一 ID 发生了变化 如 ANDROID ID 我使用了下面的 StackOverflow 链接 但现在不可用 Android 设备有唯一的 ID
  • Matlab加载库错误

    我打算将OpenCV 1 1版本和Matlab2011一起使用 该程序使用调用来加载库 但是 它返回一个错误 gt In loadlibrary at 347 In Untitled at 4 Error using loadlibrary
  • 在 Java 中将二进制输入流读入单个字节数组

    文档说不应该使用available 确定尺寸的方法InputStream 我怎样才能阅读一篇文章的全部内容InputStream到字节数组中 InputStream in assuming already present byte data
  • ActionBarCompat 菜单项未显示[重复]

    这个问题在这里已经有答案了 这是我的general xml file menu menu
  • 在 ruby​​ on Rails 中提交表单时显示结果数据,无需刷新页面

    我对 ruby on Rails 很陌生 我遇到了一个问题 我有一个主题列表 在此列表下方有一个用于添加主题的表格 我正在尝试添加一个主题而不刷新页面 并将该主题显示在主题列表的正下方 新添加的主题正在插入数据库 但如果不刷新页面 它不会显
  • TypeScript 1.5:ES6 模块默认导入 CommonJS 'export ='(.d.ts 唯一问题?)[重复]

    这个问题在这里已经有答案了 我遇到了以下问题 import moment from moment moment本身是一个默认 CommonJS 导出的函数 如此处编码https github com borisyankov Definite
  • Chrome 在没有任何用户交互的情况下立即消除确认()提示

    我们网站的一些用户报告说confirm对话框出现 但立即消失 就好像它们被自动关闭一样 这似乎只影响 Chrome 而不影响其他浏览器 甚至 Chromium 搜索类似问题发现很多人都在抱怨confirm里面的对话onbeforeunloa
  • NSMutableDictionary setObject:forKey: 无法添加密钥

    我确信我在尝试编写的一个小型iPhone程序中遗漏了一些东西 但是代码很简单并且编译时没有任何错误 所以我无法看出错误在哪里 我设置了一个 NSMutableDictionary 来存储学生的属性 每个属性都有一个唯一的键 在头文件中 我声
  • 如何自定义 git 的合并提交消息?

    每次进行合并时 我都需要生成合并提交 并且我希望它不仅仅是所有提交的摘要 我的问题是如何格式化 git fmt merge msg 或决定此自动消息的因素 我可以在提交后通过修改并使用 git log pretty format 手动执行此
  • Jetty '{servlet}/{parameter}' url 路由

    我使用的是码头9 0 3 如何将 www myweb com servlet parameter 等 URL 映射到给定的 servlet 和参数 例如 URL client 12312 将路由到 clientServlet 及其doGet
  • 如何解决返回对象的映射中的待处理承诺?

    这将解决所有承诺 const promises files map filename gt getPdfToPrint output outputDirectory filename replace const res await Prom
  • 查找mysql中整数的排名[重复]

    这个问题在这里已经有答案了 可能的重复 Mysql排名函数 我有以下国家 地区表 country clicks 0 222 66 34 175 1000 45 650 我如何获得 45 个国家 地区的排名 在本例中为 2 订购country
  • 在 driver=new ChromeDriver(); 行上获取“InitationTargetException”异常;

    我正在打开 Chrome 浏览器 并收到异常 InvocationTargetException 几天前代码运行正常 这是我的代码 System setProperty webdriver chrome driver D Automatio
  • 获取数组的所有组合

    我目前正在尝试创建一个函数来获取数组值的所有可能组合 我想出了一个非函数版本 但它仅限于 3 个值 所以我试图用它来创建一个函数 使其变得更强大Dynamic 我尝试搜索 但找不到我想要做的 powershell 示例 我可以找到 PHP
  • 如何从java中的JSONObject获取所有键/子键?

    这是我的 JSONObject per page 3 total 12 data color 98B2D1 year 2000 name cerulean id 1 pantone value 15 4020 color C74375 ye
  • 堆栈中的动态数组?

    它是否正确 这是用 g 3 4 成功编译的 int main int x 12 char pz x 这是所有其他问题的组合答案 你现在的代码是not标准C 它is标准C99 这是因为 C99 允许您以这种方式动态声明数组 澄清一下 这也是标
  • 五个按钮保持等距圆周旋转

    我想在半径为 100 的圆 以 120 120 为中心的圆 实际上是正方形视图的中心 即 240 240 的圆周上旋转五个按钮 是否可以这样做 与按钮进行交互它们是旋转的且外观正确 我努力了 x round cx redious cos a
  • Nullable 和 Int32 类型之间未定义 Equal

    我正在编写一个无聊的应用程序来管理患者及其临床病史 我将 SQLite 与 DbLinq 库和 DbMetal 代码生成实用程序结合使用 以下是从底层数据库中提取的生成代码中的两个类 Table Name main Patients pub