实体框架不保存数据

时间:2016-07-03 11:49:48

标签: c# asp.net entity-framework

我正在尝试从Entity Framework将数据保存到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();
    }
}

当我尝试上述方法时,我会看到

  

抛出异常:&#39; System.InvalidOperationException&#39;在System.Data.Entity.dll中附加信息:ObjectStateManager不包含ObjectStateEntry,该ObjectStateEntry引用了类型为&#39; portalef.tblPortalCustomerInfo&#39;的对象。

1 个答案:

答案 0 :(得分:1)

在名为GetCustById的函数中,您搜索具有特定ID的客户,如果客户不存在,则创建它然后将其返回到调用代码。无论哪种方式,客户已存在于数据库中并且调用AddObject()只会在数据库中创建一个条目如果它尚不存在