Update multiple records using web api - c#

Today in interview I got asked this kind of question.
Let's say you want to update multiple records same time using web api then how you can do that.
As it is around 1000s of records for updating..
So I give reply to use async and await for now.
Then he ask me if user 1 update record and same time user 2 update record so which will take action and how this scenario can handle.
So what should be best reply for this kind of question.

Entity framework is not designed for mass updates - it is designed for queries and transactional updates (a few records at a time).
If you need to mass-update data then either write a SQL statement that will do all of the updates or use an ETL tool like SSIS. So, raw SQL is faster than Entity framework. With that said, For normal CRUD that is not querying a billion rows, EF is totally fine, but when handling a significant amount of data, replacing EF calls with stored procedures is a good choice.

Question: If user 1 update record and same time user 2 update record so which will take action and how this scenario can handle?
Its call "concurrency", To handle this kind so scneario you have to use locking mechanism
for handling concurrency (updating same record from different user) so that user may understand that other
user is modifying this as well.
Asp.net Entity Framework Aspect:
In asp.net Entity framework has
RowVersion property which will track update log for a specific data
when it’s been updated.
Two kind of Concurrency Mechanism you can use:
Optimistic Concurrency
Pessimistic concurrency
You can have a look official document
for more details. Additionally here is the implementations
Database Aspect:
From database you can also handle this scenario. All the database also
has Locking mechanism to work on same records simultaneously by
multipole user. You can have a look here
Hope it will guide you accordingly and redeem your confusion.

Related

Multi-threading problems on Entity Framework Core

I'm using Entity Framework as my way of communicating with the database and fetching/writing information on it, on a ASP.NET CORE application. (Used as a very basic API, acting as a server for a separate application.)
There comes a time when the clients make requests to join a given lobby. I've just confirmed that if 4 requests at the same time enter, they will all be signed up on the lobby, but the player count did not update, and if it did - it'd go over the top/limit.
Am I using entity framework wrong? Is there an alternative tool to be used for such things, or should I just make it so it uses a single thread (If someone can remind me how), or encapsulate all my actions/endpoints with a lock block statement?
No matter how I structure my code, it's all prone to these same-timed http requests, moving parallelly through my repository/context.
It'd be great if I could make some kind of a queue, which I believe is what encapsulating everything in a lock would do.
EDIT:
As answered by vasily.sib, I can resolve this with the use of concurrency tokens. Please check his comment for some amazing information on how to use them!
Your problem is that operations like these...
_context.Sessions.FirstOrDefault(s => s.Id == sessionId).PlayerCount += 1;
_context.SaveChanges();
...are not atomic. With FirstOrDefault you get the session (which you dereference then without a null check, so First would be a better option here, since you will get a better error message). Then you save the changes in another step. Between those steps another concurrent thread could have changed and already saved a new value for PlayerCount.
There are multiple ways to resolve this, and most of them would require some changes on DB level.
One way to resolve it is to write a stored procedure that can do the update atomically. You would need an UPDATE statement similar to this one:
UPDATE Sessions
SET PlayerCount = PlayerCount + 1
FROM Sessions
WHERE Id = #SessionId
If you don't want to use stored procedures you could send this SQL directly to the DB.
One problem with this solution is that later you also do this step:
if (thisSession.Size == thisSession.PlayerCount)
{
thisSession.Status = SessionStatus.Active;
_context.SaveChanges(); // this can be trimmed and added at the previous scope
}
You would have to integrate this in your UPDATE statement to keep the operation atomic. If you want to add additional steps, things can get complicated.
Another way is to use optimistic concurrency built into EF core. In a nutshell, this means that when you save the data ef will first check whether the destination row is still in the same version compared to the moment you retrieved it.
To achieve that, your session needs a column that will contain a version counter. For SQL Server this would be a colun of type rowversion. Once you have this column, EF can do its optimistic concurrency magic. EF will throw a DbUpdateConcurrencyException which you would have to handle. There are different ways to resolve the error. One easy way would be to repeat your entire operation until it works out.

Application DAL design

Hello and thanks for looking.
I have a DAL question for an application I'm working on. The app is going to extract some data from 5-6 tables from a production RDBMS that serves a much more critical role in the org. What the app has to do is use the data in these tables, analyze, apply some business logic/rules and then present.
The restrictions are that since the storage model is critical in nature to the org, I need to restrict how the app will request the data. Since the tables are relatively small, I created my data access to use DataTables to load the entirety of the db tables on a fixed interval using a timer.
My questions are really around my current design and the potential use of EF or LINQtoSQL
Can EF/LS work around the restrictions of the RDBMS. Most tutorials I've seen, the storage exists solely for the application. Can access to the storage be controlled and/or can EF use DataTables rather than An RDBMS?
Since the entirety of the tables are going to be loaded, is there a best practice for creating classes to consume the data within these tables? I will have to do in memory joins and querying/logic to get at the actual data I need.
Sorry if I'm being generic. I'm more just looking for thoughts and opinions as opposed to a solution to my problem. Please done hesitate to share your thoughts. Thanks.
For your first question, yes Entity Framework can use a existing DB as it's source, the term to search for when looking for Entity Framework tutorials on this topic is called "Database First"
For your second question let me first preface it with a warning: many ORMs are not designed around using it to load the entire data table and do bulk operations on them, especially if you will be modifying the result set and pushing the data back to the server in large quanties. The updates will be row based not set based because you did the modifications in C# code, not in a T-SQL query. Most ORMs are built around the expectation that you will be doing CRUD operations on the row level, not ETL operations or set level CRUD operations (except for Read which most ORMs will do as a set operation).
If you will not be updating the data, only pulling out using Entity Framework and building reports and whatnot off of the data you should be fine. If you are bulk inserting in to the database, things get more problematic. See this SO question for more information.

Entity Framework and ADO.NET with Unit of Work pattern

We have a system built using Entity Framework 5 for creating, editing and deleting data but the problem we have is that sometimes EF is too slow or it simply isn't possible to use entity framework (Views which build data for tables based on users participating in certain groups in database, etc) and we are having to use a stored procedure to update the data.
However we have gotten ourselves into a problem where we are having to save the changes to EF in order to have the data in the database and then call the stored procedures, we can't use ITransactionScope as it always elevates to a distributed transaction and/or locks the table(s) for selects during the transaction.
We are also trying to introduce a DomainEvents pattern which will queue events and raise them after the save changes so we have the data we need in the DB but then we may end up with the first part succeeding and the second part failing.
Are there any good ways to handle this or do we need to move away from EF entirely for this scenario?
I had similar scenario . Later I break the process into small ones and use EF only, and make each small process short. Even overall time is longer, but system is easier to maintain and scale. Also I minimized joins, only update entity itself, disable EF'S AutoDetectChangesEnabled and ValidateOnSaveEnabled.
Sometimes if you look your problem in different ways, you may have better solution.
Good luck!

Batch update for RIA in SL5

c#
I need to update 4 objects (entities) that exits on a observableCollection.
if they are not bound to the view (UI)
What's the best way or How they should be updated using RIA?
I would not like to create 4 trips to the database.
Would this gerenate 4 sql update commands?
What about if there is a time frame while the User decides what to change, could be other user changing one of the entity. if so what?
Any links I could read related to these questions ?
thanks in advance
should at least know what kind of update you need to do, basing on your question I'm just assuming that you need the user to change some arbitrary values on some entities, so, no "optimizations" and Group update can be done.
The domaincontext will keep track of your changes and send them as a whole in single Changeset.
The number of trips that you'll do to the database it's not related to WCF Ria services, rather it's a feature of your data layer, however, if you are using an ORM like nHibernate take a look at it's batchsize, or for EF take a look at his extension: http://weblogs.asp.net/pwelter34/archive/2011/11/29/entity-framework-batch-update-and-future-queries.aspx
Normally yes. Any out of the box data layer solution I know of, will generate 4 distinct updates
this is known as Concurrency. Again, is something that you should manage at your data layer. Raising an exception if other user have changed that row is a reasonable way in most case
take a look at this http://blogs.infosupport.com/optimistic-concurrency-with-wcf-ria-services-and-the-entity-framework/
I suggest you to reformulate your question into more specific arguments. Actually it's too wide, each point requires analysis of your needs and it's impossible to indicate a way.

Is it possible to sync not whole table data in MS Sync Framework?

I have mobile application, so i dont want to send/receive whole changes in tables..Just some data, that meets some filter terms. Is it possible to achieve with SF; if it is, please provide some resources to read about it, because i found almost nothing.
Thank You.
Yes its possible. For example you might only want to sync the records relating to a specific store, rather than all the changes in the store table.
You do this by adding a parameter to the SyncParameters collection. e.g.
m_SyncAgent.Configuration.SyncParameters.Add("#ParamName", paramValue)
This will pass the parameter data to the serverside of the Sync process, which you can then use to sync only the data you want to include.
It's definitely possible with SQL Server Replication Services (SSRS). You can select which tables, fields, and even apply filters to the publication. I'm not familiar with Sync Framework but SSRS subscriptions appear in the Sync Center, so my assumption is that Sync Framework uses SSRS.

Categories

Resources