我试图从实体框架保存数据到SQL Server;这通常工作,我不知道我在这里做错了什么,它根本不会抛出一个错误。实体框架不保存数据
try
{
tblPortalCustomerInfo _custInfo = new tblPortalCustomerInfo();
_custInfo = _da.GetCustById(new Guid("397E4A4B-CD89-43DC-9E70-C19FC27EE6E4"));
_custInfo.addressLine1 = tbaddress1.Text;
_custInfo.addressLine2 = tbaddress2.Text;
_custInfo.town = tbtown.Text;
_custInfo.county = "test";
_custInfo.postcode = tbpostCode.Text;
_custInfo.email = tbEmail.Text;
_custInfo.phone = tbDayPhone.Text;
if(_custInfo.EntityState == System.Data.EntityState.Detached)
{
_da.portalEntities.tblPortalCustomerInfoes.AddObject(_custInfo);
}
_da.SaveChanges();
}
catch (Exception ex)
{
throw;
}
我通过代码一步,产生任何错误,它是越来越重视确定这里是我的GetCustById
功能
// <returns></returns>
public tblPortalCustomerInfo GetCustById(Guid id)
{
try
{
tblPortalCustomerInfo _customerInfo;
_customerInfo = (from _custInfo in _dal.portalEntities.tblPortalCustomerInfoes
where _custInfo.id == id
select _custInfo).FirstOrDefault();
//If the empNotes entity is null create a new one and attach it to the context.
if (_customerInfo == null)
{
_customerInfo = new tblPortalCustomerInfo();
_dal.portalEntities.tblPortalCustomerInfoes.AddObject(_customerInfo);
}
return _customerInfo;
}
catch (Exception ex)
{
string inner = string.Empty;
if (ex.InnerException != null)
{
inner = ex.InnerException.ToString();
}
return null;
}
}
我的门户实体只是关闭我的上下文基地
public portalEntities1 _portalEntities;
public portalEntities1 portalEntities
{
get
{
if (_portalEntities == null)
{
try
{
_portalEntities = new portalEntities1();
}
catch (Exception ex)
{
}
}
return _portalEntities;
}
}
之前你说我做回发检查是的,我在我的页面加载
protected void Page_Load(object sender, EventArgs e)
{
/****** Script for SelectTopNRows command from SSMS ******/
/****** Script for SelectTopNRows command from SSMS ******/
tblPortalCustomerInfo _myCustomerInfo = _da.GetCustById(new Guid("397E4A4B-CD89-43DC-9E70-C19FC27EE6E4"));
if (!IsPostBack)
{
tbaddress1.Text = _myCustomerInfo.addressLine1;
tbaddress2.Text = _myCustomerInfo.addressLine2;
tbtown.Text = _myCustomerInfo.town;
tbpostCode.Text = _myCustomerInfo.postcode;
tbMobile.Text = _myCustomerInfo.mobile;
tbEmail.Text = _myCustomerInfo.email;
tbLandLine.Text = _myCustomerInfo.phone;
}
}
我的门户网站的实体属性,它是在我的上下文基地
public portalEntities1 _portalEntities;
public portalEntities1 portalEntities
{
get
{
if (_portalEntities == null)
{
try
{
_portalEntities = new portalEntities1();
}
catch (Exception ex)
{
}
}
return _portalEntities;
}
}
我没有得到任何错误的信息是不保存到数据库某种原因,任何人有任何想法
注1
好的,我使用ef5是这种改变对象状态的正确方法,这对我来说似乎是合乎逻辑的
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
tblPortalCustomerInfo _custInfo = new tblPortalCustomerInfo();
_custInfo = _da.GetCustById(new Guid("397E4A4B-CD89-43DC-9E70-C19FC27EE6E4"));
_custInfo.addressLine1 = tbaddress1.Text;
_custInfo.addressLine2 = tbaddress2.Text;
_custInfo.town = tbtown.Text;
_custInfo.county = "test";
_custInfo.postcode = tbpostCode.Text;
_custInfo.email = tbEmail.Text;
_custInfo.phone = tbDayPhone.Text;
_da.portalEntities.ObjectStateManager.ChangeObjectState(_custInfo, System.Data.EntityState.Modified); ----> is this correct for ef5
_da.SaveChanges();
}
}
当我尝试我带有
抛出异常上述方法:在System.Data.Entity.dll附加信息“System.InvalidOperationException”:该ObjectStateManager不包含具有与参考的ObjectStateEntry一个'portalef.tblPortalCustomerInfo'类型的对象。
如果您提供的问题是用最少的代码来重现您的问题,那就太好了。另外,空的'try'-'catch'块不是一个好习惯。您可以在这里执行Debug-Exceptions并检查** CLR Exceptions **下的复选框。然后,在调试模式下,每个抛出的异常都会显示给您。你也可以在那里实现逐步记录,看看它究竟在哪里崩溃。从侧面来看,很难说为什么该代码目前不能保存实体。 –
@IvanYurchenko嗨,对不起,那个选项在哪里 – rogue39nin
按下'Ctrl + D,E',然后在** CLR异常下**检查“抛出”和“用户未处理”选项。这将帮助您确定抛出,但由catch子块异常处理。 –