2016-07-03 199 views
0

我试图从实体框架保存数据到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'类型的对象。

+2

如果您提供的问题是用最少的代码来重现您的问题,那就太好了。另外,空的'try'-'catch'块不是一个好习惯。您可以在这里执行Debug-Exceptions并检查** CLR Exceptions **下的复选框。然后,在调试模式下,每个抛出的异常都会显示给您。你也可以在那里实现逐步记录,看看它究竟在哪里崩溃。从侧面来看,很难说为什么该代码目前不能保存实体。 –

+0

@IvanYurchenko嗨,对不起,那个选项在哪里 – rogue39nin

+0

按下'Ctrl + D,E',然后在** CLR异常下**检查“抛出”和“用户未处理”选项。这将帮助您确定抛出,但由catch子块异常处理。 –

回答

1

称为GetCustById你搜索客户与特定的ID,如果客户不存在,您创建,然后将其返回给调用code.Either方式在函数内部客户已经存在于数据库调用AddObject()只会在数据库中创建一个条目如果它还不存在

+0

是的,但你注意那里,虽然这是一个很好的抓住我检查是否实体分离或不分离 – rogue39nin

+0

但你说我不需要它? – rogue39nin

+0

如果根据上下文进行分离,则无关紧要。事实是该条目已经存在于数据库中,因此调用AddObject()不会让您插入它。您需要找到插入此记录的备用方法,例如调用Add(),但你需要清除主键字段,否则你会得到一个错误,说这个PK的条目已经存在 –