none
linq中怎么解决这个问题:Cannot add an entity with a key that is already in use. RRS feed

  • 问题

  • 错误信息:Cannot add an entity with a key that is already in use.
    错误源:App_Web_vvfzwtzj
    堆栈跟踪: 在 UserManagerUC.imgBtnSubmit_Click(Object sender, ImageClickEventArgs e) 位置 d:\MyWork\IM\Controls\UserManagerUC.ascx.cs:行号 361

    C# code

      protected void imgBtnSubmit_Click(object sender, ImageClickEventArgs e)

            {

                this.DoInit();

                try

                {

                    DBIMDataContext IM = (DBIMDataContext)Application["IM"];

                   

                    try

                    {

                        var users = from user in IM.Users

                                    where user.userID == this.txtUserID.Text

                                    select user;

     

                        if (users.Count<User>()>0)

                        {

                            this.lblFailed.Text = "This user has been existed!";

                            this.lblFailed.Visible = true;

                            return;

                        }

                        if (string.IsNullOrEmpty(this.txtAddress.Text))

                        {

                            this.lblFailed.Visible = true;

                            this.lblFailed.Text = "Address is empty!";

                        }

                        else if (string.IsNullOrEmpty(this.txtCompany.Text))

                        {

                            this.lblFailed.Visible = true;

                            this.lblFailed.Text = "Company is empty!";

                        }

                        else if (string.IsNullOrEmpty(this.txtEmail.Text))

                        {

                            this.lblFailed.Visible = true;

                            this.lblFailed.Text = "Email is empty!";

                        }

                        else if (string.IsNullOrEmpty(this.txtPassword.Text))

                        {

                            this.lblFailed.Visible = true;

                            this.lblFailed.Text = "Password is empty!";

                        }

                        else if (string.IsNullOrEmpty(this.txtTelphone.Text))

                        {

                            this.lblFailed.Visible = true;

                            this.lblFailed.Text = "Telphone is empty!";

                        }

                        else

                        {

                            User xUser = new User();

                            xUser.userID = this.txtUserID.Text;

                            xUser.address = this.txtAddress.Text;

                            xUser.company = this.txtCompany.Text;

                            xUser.email = this.txtEmail.Text;

                            if (string.IsNullOrEmpty(this.txtNickName.Text))

                            {

                                xUser.nickName = this.txtUserID.Text;

                            }

                            else

                            {

                                xUser.nickName = this.txtNickName.Text;

                            }

                            xUser.orderFlag = int.Parse(this.ddlOrderFlag.SelectedValue);

                            xUser.password = this.txtPassword.Text;

                            xUser.roleID = this.rblRoles.SelectedValue;

                            xUser.telphone = this.txtTelphone.Text;

                           

                            IM.Users.InsertOnSubmit(xUser);

                            IM.SubmitChanges();

     

                            this.divList.Visible = true;

                            this.BindGVUsers();

                        }

                    }

                    catch (Exception exception)

                    {

                        throw exception;

                    }

                }

                catch (Exception exception)

                {

                    try

                    {

                        //发送错误信息给管理员

                        MyMail mail = new MyMail();

                        mail.Send(exception);

     

                        this.lblFailed.Visible = true;

                        this.lblFailed.Text = exception.Message;

                    }

                    catch (Exception ex)

                    {

                        this.lblFailed.Visible = true;

                        this.lblFailed.Text = ex.Message;

                    }

                }

              

            }

    2009年4月25日 4:14

答案

全部回复

  • 终于解决了...
    • 已标记为答案 卡洛尔 2009年4月25日 14:25
    2009年4月25日 14:25
  • 一:若数据库做了更改之后,在设计器中实体必须做一下修正。(这是罪魁祸首)
    二:检查一下数据库,与你将要变更的数据,在唯一性的字段上是否有重复项。
    三:检查一下Key的设置,一般都是有符号自增。(这样设置是绝对不会出现此错误,无符号自增在批量插入时有可能会出错)

    在DataContext未提交时候,数据都在内存中的,当提交时候,内存中的Key与服务器的Key会做对比,若是自增列,服务器会有一个ID,会自动给内存中的Key赋值并自增。若不是自增列,则会判断服务器上的Key是否已存在,若存在则会报Key已使用的异常。

    Linq中有AttachAll这个方法,这是更新一组数据的,此方法会把内存中的Key到服务器查找,若内存中Key存在重复也会报错误,因为批量时候内存中的Key会按顺序的一个一个被使用,而且事物未完成是不释放,所以当内存中Key存在重复了,就无法进行下去。若这方法更新一条记录则无此问题。
    若更新的一组记录中只要有一个Key查找不到,则看是否把asModify这个参数设为true,若设为true则提交时候直接抛出异常,事物断开,之前更新的不会滚。若设为false则不报异常,事物回滚,一组数据无一更新。
    2011年11月10日 3:35