簡體   English   中英

消息:此EntitySet中已經存在具有相同標識的實體

[英]Message: An entity with the same identity already exists in this EntitySet

添加實體時出現以下錯誤
“此實體集中已經存在具有相同標識的實體”
令人沮喪的是,這是一個間歇性的問題,對我來說很難復制。 我的代碼:

     private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        this.IsHitTestVisible = false;
        Form Form = _context.Forms.FirstOrDefault();

        Form.Transactions.Add(new Transactions
        {
            Comments = textbox_taskcomments.Text,
            By = UserID,
            Name = Name,
            IssuedOn = DateTime.Now,
            StatusID = StatusID
        });

        this.DialogResult = DevExpress.Xpf.Core.DialogResult.OK;
        this.Close();
    }

任何幫助將不勝感激,因為我不知道從哪里開始。

謝謝

您是否嘗試過contains方法?

包含:指定EntitySet是否包含特定實體。

在修改它之前,您應該檢查EntitySet的內容。

 Transactions t = new Transactions{
        Comments = textbox_taskcomments.Text,
        By = UserID,
        Name = Name,
        IssuedOn = DateTime.Now,
        StatusID = StatusID
 };
 //check that the entity does not yet contain t
 if(!Form.Transactions.Contains(t)){
 //do something.
 }

(我不知道您的其余代碼,但是..根據我的閱讀 ,這可能是一個解決方案)

暫無
暫無

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

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