簡體   English   中英

實體框架未保存數據

[英]Entity Framework is not saving data

我正在嘗試將數據從實體框架保存到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不包含引用了類型為“ portalef.tblPortalCustomerInfo”的對象的ObjectStateEntry。

在名為GetCustById的函數中,您將搜索具有特定ID的客戶,如果該客戶不存在,則創建該客戶,然后將其返回給調用代碼。無論哪種方式,該客戶已存在於數據庫中並僅調用AddObject()在數據庫中創建一個條目( 如果尚不存在)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM