ASP. NET Core EF core MySQL new data concurrency exception handling

Source: Internet
Author: User

NET Core 2.0 was released, always wanted to experience, for a variety of reasons, has been dragging not.

Before the company to add a new internal management background, just can be done with the ASP. NET core, experience the charm of the next.

After chewing the document, I got started and everything went well.

Until Friday, there was a problem of concurrency, this thought can be quickly disposed of, but did not expect to spend a long time to solve, now record the situation, have the same experience of the partners later can be consulted.

First on the exception.

Exception prompt:

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:Database operation expected to affect 1 row (s) but actually Affected 0 row (s). Data may have been modified or deleted since entities were loaded. See

http://go.microsoft.com/fwlink/?LinkId=527962

For information on understanding and handling optimistic concurrency exceptions.

Code again.

 public int Add (Bannerview bannerview) {try {//sysbanner.imgurl = Sysbanner. Imgurl.contains ("http")?                Sysbanner.imgurl: _programconfig.aliyunossurl + sysbanner.imgurl;                Sysbanner.createdate = DateTime.Now; _banner.                    ADD (new Sysbanner {Content = bannerview.content, CreateDate = DateTime.Now, Del = (int) Bannerview.del, Imgurl = BannerView.ImgUrl.Contains ("http")?                    Bannerview.imgurl: _programconfig.aliyunossurl + bannerview.imgurl, operation = "",                    Sort = bannerview.sort, Srcurl = bannerview.srcurl, Title = Bannerview.title,                    Type = (int) bannerview.type,//materialid = 0,//parameter = 0,                Sharetype = 0}); Return _banner.                       SaveChanges ();     } catch (DBConcurrencyException ex) {throw; }        }

  

        <summary>////Add a new data////</summary>//        <param name= "model" ></param>        public void Add (TEntity model)        {            var entity=_dbset.add (model);        }       public int SaveChanges ()        {            return _dbcontext.savechanges ();        }        

The code is especially simple: add a piece of data, save it, nothing complicated.

A concurrency exception occurs, and the whole person is in a state of force.

Single-user debugging, add a new data, self-increment ID, there is no multiuser simultaneous operation, how can there be concurrency?

Even if concurrency occurs, the primary key is also the self-increment ID, and only multiple data will appear, and it cannot be a concurrency exception ... and multiple business modules, the addition of other modules is completely normal.

This is again why!!!

The first is to look at the code and make sure it's OK ... Check for Dependency injection, no problem ... Check the SQL generated by EF, no problem ... Read the official documentation and find out why ... To the group of friends to consult, or can not solve.

Official documentation There is a dbconcurrencyexception that handles concurrency exceptions, which are based on modifications and updates, and are not solved by experimentation.

Step by step debugging, source debugging, find a few points of doubt, after the investigation is not caused by the new concurrency bug.

Re-dbfrist Generate model entity, cannot solve ... Delete table, rebuild table, cannot be resolved.

Seems to enter a dead end, the garden Bo asked posts, also did not solve. The whole process of elimination, said all is tears.

Later on, since the underlying invocation method is the same, the only difference is the stored entity, will it be caused by the table structure?

After comparing the table with the normal table structure, found a different point, there are several fields set the default value, there are two non-null fields, and the normal operation of those entities are not.

After trying to cancel the default value, then comment out the dbfirst generated line of code hasdefaultvaluesql ("XXX"), and then a run, incredibly successful!!!

Is it the EF core MySQL bug? Table cannot set default values? This seems unreasonable, product release so long, many people have been used in the production environment, it is impossible to make such a low-level error it?

In the garden Bo asked the post, a friend said to me, the default value of the field, to add. Hasdefaultvalue (). This is code first, and when I use Dbfirst, I automatically generate Hasdefaultvaluesql ("xxx").

Is it a bug that occurs when you call a function differently?

After many actual tests, it is no problem to set default values and non-null fields in the database.

The problem is hasdefaultvaluesql ("xxx") on this line of code that is automatically added to the DB first build entity, just comment out the code and run normally.

. Hasdefaultvalue () can cause new concurrency exceptions, for unknown reasons.

ASP. NET Core 2.0

EF Core MySQL

Pomelo.entityFrameworkCore.mySql

If a friend encounters a similar problem, it can be used as a reference.

As for why it is not possible to use Hasdefaultvaluesql ("XXX") and.Hasdefaultvalue (), it is currently being studied, and there are new discoveries that will update the article.

ASP. NET Core EF core MySQL new data concurrency exception handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.