的Int32>等于没有可空类型℃之间定义;和的Int32 [英] Equal is not defined between type Nullable<Int32> and Int32

查看:362
本文介绍了的Int32>等于没有可空类型℃之间定义;和的Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个无聊的应用程序来管理病人及其诊所的历史。我使用的SQLite与图书馆的DBLinq和DbMetal代码生成工具相结合。下面是从底层数据库中提取的genereated码两类:

I am writing a boring application to manage patients and their clinic history. I used SQLite combined with DbLinq libraries and DbMetal code generation utility. Here are two classes from the genereated code extracted from the underlying database:

[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));
        }
    }
}



我用下面的代码地址添加到病人的:

I use the following code to add an address to a patient:

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,我收到此异常:

Database.Source is an instance of the class that extends DataContext in the generated code. On SubmitChanges, I receive this exception:

等于运营商不能为空(作者Int32)已和的Int32之间定义的

"Equal operator is not defined between Nullable(Of Int32) and Int32."

该消息不报一字一句,但意思是一样的。堆栈跟踪点代码的DBLinq,更精确地线源文件DbLinq.Data.Linq.DataContext.cs的709。你可以在这里找到源文件: http://dblinq.codeplex.com/SourceControl /变更/查看/ 16800#314775 (方法SetEntityRefQueries的身下(对象实体))。
我看到,在表达式树比较具有恒定外键值时,问题就来了,但我couln't设法得到上的其他信息。你能帮我找到问题

The message is not reported word by word, but the meaning is the same. The stack trace point to DbLinq code, more precisely to line 709 of source file DbLinq.Data.Linq.DataContext.cs. You can find the source files here: http://dblinq.codeplex.com/SourceControl/changeset/view/16800#314775 (under the body of the method SetEntityRefQueries(object entity)). I see that the problem comes when comparing a foreign key value with a constant in an expression tree, but I couln't manage to get other information on that. Can you help me find the issue?

注意:?现场address.PatientID(外键)实际上是设定的SubmitChanges的调用之前正确的值

N.B.: the field address.PatientID (foreign key) is actually set to the correct value before the invocation of SubmitChanges.

推荐答案

正如我在上面的注释(这我在此重复这样我就可以链接图像)提到,你的主键不应该可为空。应该有在映射属性,您可以更改设置它,虽然我不使用的DBLinq,所以我不能直接给你的截图。相反,在这里它是在LINQ-2-SQL DBML设计师(左)和实体框架EDMX设计(右)。

As I mentioned in the comment above (which I'm repeating here so I can link images), your primary key should not be nullable. There should be a property in your mapping that you can change to set it, although I don't use DbLinq, so I can't give you a screenshot of it directly. Instead, here it is in the LINQ-2-SQL DBML designer (left) and the Entity Framework EDMX designer (right).

我没有那么肯定你删除的问题 - 这似乎是它应该工作给我。您可以编辑您的问题,包括你删除整个代码块?作为初步猜测,你要么创建一个新的对象(加载酮代替),然后试图将其删除,或者你删除,而不删除对象的关联。

I'm not as sure about your deletion problem - that seems like it should work to me. Can you edit your question to include the whole block of your deletion code? As a preliminary guess, you're either creating a new object (instead of loading one) and then trying to delete it, or you're deleting the association without deleting the object.

作为一般规则,我从来没有从数据库中删除,如果我能避免它 - 我只是标记为非活动状态。这是很容易为取消删除的方式。

As a general rule, I never delete from a database when I can avoid it - I just mark inactive. It's much easier to "undelete" that way.

这篇关于的Int32&GT;等于没有可空类型℃之间定义;和的Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆