System.Data.Linq.ChangeConflictException: Row not found or changed

后端 未结 19 2673
生来不讨喜
生来不讨喜 2020-12-07 22:21

I am trying to delete a selected gridview row using LINQ (No LINQDataSource).

When the selection is changed, the detailsview binding is changed also. I can add a new

相关标签:
19条回答
  • 2020-12-07 23:05

    I'm having the same problem, and came across this blog, which basically states that Linq-To-Sql has got a problem in it's optimistic concurrency where:

    1. High precision datetime fields are used. The solution is to set UpdateCheck to never for that column your DBML file
    2. GridView columns which are set to invisible are accessing a property on the data object (this second reason makes no sense, but it seems to be all the rage on that blog).

    I haven't tried these solutions yet, but will post back here once I have.

    0 讨论(0)
  • 2020-12-07 23:07

    I had similar changeconflictexception/"Row not found or changed" when updating a row. Solved it by re-adding the tabbles in the dbml.

    0 讨论(0)
  • 2020-12-07 23:08

    For us the problem started when we switched to DateTime2 on the SQL Server side. Just marking fields with Column(DbType = "DateTime2") did not help. So, what happened was that initially on the database side we declared our columns as DateTime2(3) to be "backward compatible" with the old DateTime type and everything seemed to work fine until we noticed that when we use SQL-2-Linq we get the "Row not found or changed" exception in the updates of those date fields. To make the quite-long story short the solution was to do 2 things:

    1. Mark the columns with [Column(DbType = "DateTime2(3)", CanBeNull = false)] to match the database declaration. AND
    2. Trim the extra precision digits from the properties in setters like so:

    [Column(DbType = "DateTime2(3)", CanBeNull = false)] public DateTime ModifiedAt { get => _modifiedAt; set => _modifiedAt = value.AddTicks(-(value.Ticks % TimeSpan.TicksPerMillisecond)); }

    0 讨论(0)
  • 2020-12-07 23:10

    The problem I had was that I had a DateTime type in the .net framework, but our database field was of DateTime2 type, which is higher precision datatype. So when we would submit changes the date fields of the object vs. the DB was just a few nanoseconds off which would cause the concurrency error. This happened when we migrated to a newer MSSQL version and it converted our DateTime fields to DateTime2.

    So in our code where we had:

        Obj.DateUpdated = DateTime.Now()
    

    We changed it to:

        Obj.DateUpdated = DateTime.Parse(DateTime.Now.ToString())
    

    So check your datatypes, especially your date fields, if you get this error after making an upgrade and or migration.

    0 讨论(0)
  • 2020-12-07 23:10

    I was able to resolve this issue by executing databind() on the gridview and datasource during updatepanel postback.

        protected void UpdatePanel1_Load(object sender, EventArgs e)
        {
            GridView1.DataBind();
            LinqDataSource1.DataBind();
        }
    

    I refresh updatepanel each time my selection index changes and it was able to resolve the conflicts.

    Hope this helps.

    0 讨论(0)
  • 2020-12-07 23:10

    Under:

    QuestionModule o = query.First();
    

    You have to add the following command:

    db.QuestionModule.Attach(o);
    
    0 讨论(0)
提交回复
热议问题