0

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'.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • 2
    It would be nice if you provided a question with minimal code that is reproducing your issue. Also, empty `try`-`catch` blocks are not a good practice. What you can do here is go to the Debug-Exceptions and check the checkbox under **CLR Exceptions**. Then every thrown exception will be shown to you in the debug mode. Also you can implement step-by-step logging there to see where exactly does it crash. It’s difficult to say from the side perspective why that code doesn’t save entities currently. – Ivan Yurchenko Jul 03 '16 at 11:57
  • @IvanYurchenko Hi sorry where is that option – c-sharp-and-swiftui-devni Jul 03 '16 at 12:03
  • Press `Ctrl+D,E` and there under **CLR Exceptions** check both ”thrown” and ”user-unhandled” options. This will help you to determine thrown but handled by `catch` block exceptions. – Ivan Yurchenko Jul 03 '16 at 12:36
  • @IvanYurchenko i did that but still no error being produced its simply just not saving the record – c-sharp-and-swiftui-devni Jul 03 '16 at 12:39
  • Consider using Modified flag instead - as per [this example](http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record) – Daniel Protopopov Jul 03 '16 at 13:14
  • @DanielProtopopov please see Note 1 in my edits above i treid above but its not working saving the object still no error im afriad – c-sharp-and-swiftui-devni Jul 03 '16 at 14:07

1 Answers1

1

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

Denys Wessels
  • 16,829
  • 14
  • 80
  • 120