C# 发生引用完整性约束冲突。更新EF时
我似乎无法更新具有foreignKey约束的模型,但出现以下错误: 附加信息:引用完整性约束冲突 发生:服务器一端的“Country.Id”属性值 关系与“Setting.CountryId”的属性值不匹配 在另一端 设置模型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 {
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
属性中有什么内容。