[英]Can't insert new Entity Framework entity using “.Add()”
我正在嘗試將新的Person
保存到數據庫。 我的代碼可以很好地編譯,但是當我運行它時,在.Add()
處出現錯誤。
該錯誤顯示:“ This EntitySet of Type 'Diavik.DataAccess.Person' does not support the 'Add' operation.
”
這是一個SilverLight應用程序,此文件是App.xaml.cs
這是我的代碼:
private void OnGetPerson_Completed(LoadOperation<Person> operation)
{
Person person = operation.Entities.SingleOrDefault();
if (person == null)
{
person = new Person()
{
FirstName = WebContext.Current.User.FirstName,
LastName = WebContext.Current.User.LastName,
IlluminatorLogin = WebContext.Current.User.Name
};
Context.Persons.Add(person);
}
Context.SubmitChanges(submitOp =>
{
// Some Stuff
}, null);
}
謝謝您的幫助,
亞倫
您需要在域服務中將一個方法標記為[Insert]
方法。 它必須是public
, void
,並且只能接受一個參數( Person
對象)。
像這樣:
[Insert]
public void InsertPerson(Person p)
{
ObjectContext.Persons.AddObject(p);
}
這段代碼可能並不是您所需要的,因為我不知道您的域服務是什么樣子,但是您可以大致了解。
實際上,您實際上不需要直接調用此服務方法,RIA Services鏈接可處理該方法與在客戶端上調用Persons.Add()
時的轉換。 它只是必須存在,僅此而已。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.