我在Windows窗體應用程序(vs2010,.net 4)中首次使用實體框架(數據庫優先,實體框架5)。我遇到了我的實體對象和windows窗體控件之間的綁定問題。我有文本框,datetimepicker和組合框控件。當我用綁定的控件打開窗口時,正確的數據顯示在控件中。但是,當我更改其中一個控件的值並將控件關閉時,該值將恢復爲控件中的原始值,就好像該值未被推送到對象一樣。下面是代碼exerpts:Windows窗體控件的問題綁定實體框架5對象
我的實體對象:
namespace Entities
{
using System;
using System.Collections.Generic;
public partial class ExternalDocument
{
public int ExternalDocumentID { get; set; }
public bool Active { get; set; }
public bool Closed { get; set; }
public Nullable<int> CompanyID { get; set; }
public Nullable<int> ContactID { get; set; }
public string DocumentNbr { get; set; }
public Nullable<System.DateTime> DocumentDate { get; set; }
public Nullable<System.DateTime> DateReceived { get; set; }
public virtual Company Company { get; set; }
public virtual Contact Contact { get; set; }
}
}
數據綁定:
private void SetDataBindings()
{
LoadComboBoxValues();
this.textDocumentNbr.DataBindings.Add("Text", this.document, "DocumentNbr");
this.textDocumentNbr.Leave += new EventHandler(textDocumentNbr_Leave);
this.dateDocument.DataBindings.Add(new Binding("Value", this.document, "DocumentDate"));
this.dateReceived.DataBindings.Add("Value", this.document, "DateReceived");
this.comboCompanyID.DataBindings.Add("SelectedValue", document, "CompanyID");
}
,如果有一個實體框架的錯誤,我想知道當對象屬性設置,但我一直沒有找到一個很好的方法來捕捉任何這樣的錯誤。我的實體框架對象沒有On < PropertyName>更改爲早期版本的實體框架創建的方法。我一直試圖陷入錯誤,當焦點離開控制,但認爲這不可能是最好的方法:
private void dateDocument_Leave(object sender, EventArgs e)
{
string errorString = this.entitiesController.GetValidationErrors();
this.errorDocumentDate.SetError(this.dateDocument, errorString);
}
public string GetValidationErrors()
{
string errorString = "";
List<DbEntityValidationResult> errorList = (List<DbEntityValidationResult>)this.finesse2Context.GetValidationErrors();
if (errorList.Count > 0)
{
foreach(var eve in errorList)
{
errorString += "Entity of type " + eve.Entry.Entity.GetType().Name + " in state" + eve.Entry.State + " has the following validation errors:"; ;
foreach (var ve in eve.ValidationErrors)
{
errorString += "- Property: " + ve.PropertyName + " Error: " + ve.ErrorMessage;
}
}
}
return errorString;
}
任何幫助,將不勝感激。謝謝!