Bulk update in entity framework in web api - entity-framework

I am working on a web api project, where client application will send bulk records to api to update into db. I am using C# and Entity framework.
Client application will send list of records
[
'DAD88F3D-518E-47CC-A5D9-33E15D8373A7',
'B3960124-34CF-445C-A1A7-3F1ABB383C01',
'6883E1BE-218E-499E-AC6D-13E9E7D099A9',
'A2B6D337-A615-4269-9C0F-D24D2479012B',
'A2B6D337-A615-4269-9C0F-D24D2479012B',
'4950C8EC-A6CC-42B7-AD2A-E029E7FCF11A',
'C1C477BD-B1DF-45D6-85C4-A381DC596524',
'C1C477BD-B1DF-45D6-85C4-A381DC596524',
'4938EB64-C795-46B9-B42D-D48F32AD8DF4'
]
This list would have approx 100 records at a time.
So API has to update multiple db tables with all matching Ids in the list and returns status back to client for each Ids that which Update operation is success and which is failed. Client application has to do some operation based on success or failed status.
Update tableName set Deleted = 'Y' where id in (above list )
Update anotherTableName set enrollment='Incomplete' where id in (above list)
So how it can be done in entity framework for more records update.

Disclaimer: I'm the owner of the project Entity Framework Plus
If you are looking to update multiple rows to the same value, this free library will do the job.
context.YourDbSet.Where(x => listIds.Contains(x => x.Id))
.Update(x => x.Deleted = 'Y');
Disclaimer: I'm the owner of the project Entity Framework Extensions
(This library is not free)
If you need to perform BulkUpdate in a list, you can use this library
Example
// Easiest way
context.BulkSaveChanges(); // as your normally do with SaveChanges
// Fastest way
context.BulkUpdate(list);

Related

Entity Framework ValueGeneratedOnAdd with no requery

Say I have EntityFramework Core 2.1.14.
Say I have to integrate data into a legacy MySql database which was created by a self-taught.
Say also that one of the tables in this database has a surrogate key field that is generated on Add.
Say then that I want to use context.SaveChanges() to handle the Insert DML generation for me because I want to be lazy and because these tables are messy.
Say finally that I do not want Entity Framework to perform a follow-up query to retrieve this surrogate field; I don't care what it is. I just need it generated on insert.
How would one call context.SaveChanges() with an Added object having a property configured as .ValueGeneratedOnAdd() but also instruct Entity Framework to do nothing but generate my INSERT ? I don't want it to return the id.

.NET Core Entity Framework Core: when using take, how to get totalrecords as well

I am using ASP.NET Core with Entity Framework Core.
var orglist = _dbContext.Organization
.Where(c => c.OrgName.ToLower().Contains(searchBusinessText))
.ToList().Take(100);
How do I indicate to the users that there are more than 100 hundred records based on their search criteria and that user should try to narrow down the search?
I only need to alert user if the search condition has more records.

EF Core - create database programmatically

What's the best approach to create a database in EF core programmatically and then run the migrations to make sure the new database has all database objects?
We're building a SaaS based application where subscribers can signup on the website themselves and when they signup providing correct payment details, the application needs to create a new database (in SQL Azure) for the subscriber automatically and execute all migrations on the new database.
I have looked at this question here:
auto create database in Entity Framework Core
It does not give details on the following:
1) how to specify the name of the new database
2) create a new database login
3) add that new database login to the database as a user with dbo permissions
Create an Azure SQL Database Basic tier (for example, it cost $5 a month, you can also create a free one available for a year) with all the objects needed on the database, which you can copy asynchronously with a statement like below or using PowerShell.
CREATE DATABASE db_copy
AS COPY OF ozabzw7545.db_original ( SERVICE_OBJECTIVE = 'P2' );
You can then monitor when the copy finishes with below statement:
Select
[sys].[databases].[name],
[sys].[databases].[state_desc],
[sys].[dm_database_copies].[start_date],
[sys].[dm_database_copies].[modify_date],
[sys].[dm_database_copies].[percent_complete],
[sys].[dm_database_copies].[error_code],
[sys].[dm_database_copies].[error_desc],
[sys].[dm_database_copies].[error_severity],
[sys].[dm_database_copies].[error_state]
From
[sys].[databases]
Left
Outer
Join
[sys].[dm_database_copies]
On
[sys].[databases].[database_id] = [sys].[dm_database_copies].[database_id]
Where
[sys].[databases].[name] = 'db_copy'

Entity Framework - Last ID of Entree

Currently I am programming an application which saves and deletes entries in a database (code first with entity framework). My question is how can I get the next database id (configured with auto increment - so it added automatically +1).
I tried something like:
var a = databaseContext.MyObject.LastOrDefault().Id;
var myNextDatabaseId = a+1;
This pseudo code is working for most of the cases. But if i had 5 entrees in my database and delete all the five entree's my next database-counter would be 6. When i'm using the code above it will return "null" becouse there is really no entree. But i must get the next database auto increment id.
Is there a possibility which doesn't create a new database entry? Think this shouldn't be necessary.
For example following data construct:
Inserted entry one (internal id = 1)
Inserted entry two (internal id = 2)
Delete entry one
Delete entry two
Read last database entry (entry = null). All data was deleted but I am trying to receive the next auto-increment id 3
Use an explicit transaction scope and identity column. Trying to maintain IDs at the application level isn't scale-able and will be prone to concurrency errors.
Ideally the transaction should span the web service call to roll back automatically if the call fails. With an explicit transaction you can call your context.SaveChanges() which will expose the next ID assigned to the entity, and then roll back if the web service call fails. This can be a transaction managed by the context or TransactionScope.
see: https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

Sails framework Need to fetch data from available table in database

I am using sails framework to fetch data from a table and show it to the user, after some manupulations. The table belongs to another application, I just need to read the data from it.
I created a Model same as its available in the database along with the column names. I get an error as
RequestError: There is already an object named 'table_Name' in the database.
I cannot create table, is there a way around in sails