Reputation: 3980
I am trying to save data to SQL Server from Entity Framework; this normally works and I don't know what I doing wrong here it's not throwing an error at all.
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;
}
I have step through the code and no error is produced and it is getting attached ok here is my GetCustById
function
// <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;
}
}
My portal entities is just off my context base
public portalEntities1 _portalEntities;
public portalEntities1 portalEntities
{
get
{
if (_portalEntities == null)
{
try
{
_portalEntities = new portalEntities1();
}
catch (Exception ex)
{
}
}
return _portalEntities;
}
}
Before you say am I doing a postback check yes I am here on my page load
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;
}
}
My portal entities property which is in my context base
public portalEntities1 _portalEntities;
public portalEntities1 portalEntities
{
get
{
if (_portalEntities == null)
{
try
{
_portalEntities = new portalEntities1();
}
catch (Exception ex)
{
}
}
return _portalEntities;
}
}
I get no error the information is just not saved to the db some reason anyone got any ideas
Note 1
Ok so I am using ef5 is this the right method to change a object state it seemed logical to me
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();
}
}
When I try the above method I am presented with
Exception thrown: 'System.InvalidOperationException' in System.Data.Entity.dll Additional information: The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'portalef.tblPortalCustomerInfo'.
Upvotes: 0
Views: 1273
Reputation: 17049
Inside the function called GetCustById
you search for a customer with a specific id and if the customer doesn't exist you create it and then return it to the calling code.Either way the customer already exists in the database and calling AddObject() will only create an entry in the database if it does not yet exist
Upvotes: 1