Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发生引用完整性约束冲突。更新EF时_C#_.net_Winforms_Entity Framework - Fatal编程技术网

C# 发生引用完整性约束冲突。更新EF时

C# 发生引用完整性约束冲突。更新EF时,c#,.net,winforms,entity-framework,C#,.net,Winforms,Entity Framework,我似乎无法更新具有foreignKey约束的模型,但出现以下错误: 附加信息:引用完整性约束冲突 发生:服务器一端的“Country.Id”属性值 关系与“Setting.CountryId”的属性值不匹配 在另一端 设置模型 namespace Domain { public class Setting : BaseModel { public string Address { get; set; } public string Email {

我似乎无法更新具有foreignKey约束的模型,但出现以下错误:

附加信息:引用完整性约束冲突 发生:服务器一端的“Country.Id”属性值 关系与“Setting.CountryId”的属性值不匹配 在另一端

设置模型

namespace Domain
{
    public class Setting : BaseModel
    {

        public string Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Website { get; set; }
        public string Slogan { get; set; }
        public byte[] Logo { get; set; }

        public string City { get; set; }
        public string RegistrationNo { get; set; }

        [ForeignKey("State")]
        public int StateId { get; set; }
        public  State State { get; set; }

        [ForeignKey("Country")]
        public int CountryId { get; set; }
        public  Country Country { get; set; }

        public string IsDefault { get; set; }


    }
}
namespace Domain
{
    public class State :BaseModel
    {
        public string Name { get; set; }
    }
}
namespace Domain
{
    public class Country : BaseModel
    {
        public string Name { get; set; }
    }
}
状态模型

namespace Domain
{
    public class Setting : BaseModel
    {

        public string Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Website { get; set; }
        public string Slogan { get; set; }
        public byte[] Logo { get; set; }

        public string City { get; set; }
        public string RegistrationNo { get; set; }

        [ForeignKey("State")]
        public int StateId { get; set; }
        public  State State { get; set; }

        [ForeignKey("Country")]
        public int CountryId { get; set; }
        public  Country Country { get; set; }

        public string IsDefault { get; set; }


    }
}
namespace Domain
{
    public class State :BaseModel
    {
        public string Name { get; set; }
    }
}
namespace Domain
{
    public class Country : BaseModel
    {
        public string Name { get; set; }
    }
}
国家/地区模式

namespace Domain
{
    public class Setting : BaseModel
    {

        public string Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Website { get; set; }
        public string Slogan { get; set; }
        public byte[] Logo { get; set; }

        public string City { get; set; }
        public string RegistrationNo { get; set; }

        [ForeignKey("State")]
        public int StateId { get; set; }
        public  State State { get; set; }

        [ForeignKey("Country")]
        public int CountryId { get; set; }
        public  Country Country { get; set; }

        public string IsDefault { get; set; }


    }
}
namespace Domain
{
    public class State :BaseModel
    {
        public string Name { get; set; }
    }
}
namespace Domain
{
    public class Country : BaseModel
    {
        public string Name { get; set; }
    }
}
要获取和更新设置的存储库

public Setting GetSetting()
{
    try
    {
        return _db.Settings.Include("Country").Include("State").First();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

public Setting UpdateSetting(Setting setting)
{
    try
    {
        _db.Settings.Attach(setting);
        _db.Entry(setting).State = EntityState.Modified;
        _db.SaveChanges();

        return setting;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }

}
注意我使用的是WCF代理

按钮点击事件

private void btnSave_Click(object sender, EventArgs e)
{
    //MessageBox.Show(Convert.ToInt16(cmbState.EditValue).ToString());
    //return;

    if (dxValidationProvider1.Validate())
    {
        if (picLogo.Image == null)
        {
            XtraMessageBox.Show("Upload Logo");
        }
        else
        {
            var setting = proxy.GetSetting();
            //MessageBox.Show(cmbState.EditValue.ToString()); return;

            setting.HotelName = txtHotelName.Text;
            setting.Address = txtAddress.Text;
            setting.Email = txtEmail.Text;
            setting.Phone = txtPhone.Text;
            setting.Website = txtWebsite.Text;
            setting.Slogan = txtSlogan.Text;
            setting.City = txtCity.Text;

            setting.CountryId = Convert.ToInt16(cmbCounty.EditValue);

            setting.StateId = Convert.ToInt16(cmbState.EditValue));

            setting.Logo = picLogo.Image.ToByteArray();

            var s = proxy.UpdateSetting(setting);

            MessageBox.Show(@"Updated");
        }
    }
    else
    {
        MessageBox.Show("Please fill the required fields");
    }
}

我刚刚从GetSetting函数中删除了Include语句,并更新了所有内容。这解决了我的问题

public Setting GetSetting()
{
    try
    {
        return _db.Settings.First();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

CountryId
Int32
,但您正在将
cmbcorry.EditValue
转换为
Int16
,并尝试将其分配给
CountryId
。它们是不同的类型。我尝试过得到相同的错误我假设
BaseModel
中的属性
Id
int
Int32
?BaseModel中的Id是intFirst,您似乎在使用非标准(DevExpress)控件。其次,不清楚组合框的
EditValue
属性中有什么内容。