Bulk delete in Entity framework using Entity Sql - entity-framework-5

I want to implement bulk delete in Entity framework. I am planning to use Entity sql for that, but after a long search i am not able to find a single example of delete statement in Entity Sql. I dont want to use Sql statement also. Does Entity framework provide any bulk update without using For each loop internally ? I want to delete complete list of object in single transaction. Is it possible in Entity Sql ?

There is a nuget package that provides bulk delete/update that I know of.
Most people using entity framework do not use this because it bypasses any logic built in the model (no code executed in the savechanges override for example), but if you are only doing data retrieval/delete without any business logic this might help.

Related

What is the preferred strategy/workaround for introducing legacy database entities in Jhipster

Is there a good practice to introduce existing legacy database tables/entities into a JHipster project?
The idea I had was to create the JPA entities that look exactly like the existing entities and then use the generated artifacts as per normal. Will this possibly work?
In the end, I opted for just creating a view of all the underlying tables I needed in my query. The steps I followed were:
create the view on the DB,
Generate the entity using JHipster, with exactly the same fields.
Strip out the Update ad Create and Delete functions and we have a read-only view page with all the data you need and all the fields displaying, done.
Now I can add my own routers and buttons, to allow other specific operations/flow. This page was my inspiration: https://www.javabullets.com/calling-database-views-from-spring-data-jpa/

Using OrganizationServiceContext to update a different custom entity

Can I use the existing OrganizationServiceContext for a plugin to update an entity other than the current target entity?
Yes, and if you're running pre or post transaction (not pre-validation) it'll be included as a part of the SQL transaction.

MVC 4 unable to perform scaffolding

I'm new to MVC, and my intention actually is to learn 'Code First' technique, using MVC 4. I'd been through several 'MVC 4 Entity Framework' tutorials, and no doubt, tutorials are really simple, and understandable. but for every tutorial I followd, the
EF will look for the database in the default location and if it doesn’t exist, it will create it for you
part is not working for me. Do i have to add ADO.net Entity framework model in project, and design the database entities firt? or am I missing something? It's been more than 15 days, & I'm stuck.
(I tried making database manually first, and it worked perfect. But I want to learn, as described in tutorials, that databases are automatically created, if we mention the connection string, and EntityFramework finds out that path doesn't actually contains such database, so it creates one).
I'd been through several tutorials, let's say, I'm following THIS one.
(I'm using Visual Studio Express for web 2012, EF 5.0.0)
your question: Do i have to add ADO.net Entity framework model in project, and design the database entities firt?
yes, in code first approach you need to tell entity framework what tables you are going to create, you do it by creating poco classes(Plain old CLR Object). then in run time entity framework will create a model(witch is a xml file) and generate query according to that xml file then executes it. you can read more about that in [CSDL MSL CSDL][1]
so for summary you write your domain classes(poco classes) and ef will create db for you but btw there is another way where in entity framework code first witch is called [Code first to existing database][2]
[2]: http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx it revers engineer the databse and create model for you.
Do i have to add ADO.net Entity framework model in project, and design the database entities first?
This is really the more traditional approach, aptly called the database-first approach. The code-first approach is an alternative to the former. They mean exactly what they say. In the case of code-first the code is first written and in effect the database is generated automatically. In database approach, the database is first created then you can use EF to automatically generate the code required to handle the database, mainly CRUD and connection management.
The built-in scaffolder in ASP.NET MVC should work fine either in code-first or database-first as long as you have your models already established in your project. If you find the generated code too simplistic, an alternative scaffolder that you can use is CamoteQ - it is an online ASP.NET MVC scaffolding tool that is based in DDD principles and recognized patterns.

Entity Framework Partial Entity Update

I am using Asp.net Webapi and EF and i am having issues with partial updates with the Entity Framework 5 .
I usually get say 20 fields from REST services that i need to update out of the complete entity.When I update I set the entity state to Modified.
The whole entity is updated all the time overriding all the existing values in the table that should not be changed as a result of the update.
There are random fields that I got from REST services not fixed fields that can help me to identify what properties are updated.
I want to know how this issue is being resolved out there and what are the practices are used to fix this.
Thanks.

VS 2012 EF 5 using .net 4 and database first Layered applications concepts

I upgraded from VS2005 to VS2012 specifically to code in .Net 4,.Net 4.5, and use EF5. I am new to EF. I have Julie's EF book but it talks mainly about EF4 and POCO and it has me confused trying to implement EF5.
My hosting only allows me to run .Net 4 currently. I am trying to use EF5 on a new database.
I tried Code first to generate my database but I could not get it to generate the database. So I created the database first and then generated the model from the database with EF5. What I want is a layered website using ASP.NET Forms, Data Access Layer, and a Business Layer. The business layer I intend to build so I can use ObjectDataSource to pull in to the webpage along with some web services to use cascading dropdowns.
I have my NameX.edm model created but the tt files don't have the same names, they have Model1.tt and Model1.Context.tt is not the same name, why. Also Model1.Context.tt is empty. I am attempting to create the POCO classes running the EF5 DbContext but I am having trouble getting it to see the edm model. I read somewhere that EF5 creates the POCO automatically. I am not seeing the big picture here. Is my issue that I am not using .Net4.5?
What I have is a ASP.NET Application project, a EF Project, a POCO Project, and intend to have a forth project with the Business logic. How do I get the EF generated correctly and the POCO classes? I want a layered application for to scale well. Next question How to get the POCO classes to talk with the Business logic? My database is SQL. I am use to writing stored proceedures to accomplish everything through business logic.
If you are using Model First technique, make sure that you have references to System.Data.Entity and System.Data.Entity.Design. Also, you will need to create a reference to the EntityFramework.dll as well. It is located by default in c:\Program Files\Microsoft ADO.NET Entity Framework Feature CTP5\Binaries\EntityFramework.dll. If you can not find it, make sure you have installed the Entity Framework through your Package Manager Console. To do this, open up Tools, Library Package Manager, Package Manager Console, then type Install-Package EntityFrameWork.
Next you want to add a new item to your project. Right click on your solution, go to Add, then select New Item. You want to select Data from the Common Items section on the left, then select ADO.NET Entity Data Model.
Next Select Generate From Database. In the next screen, if you have not set up your connection to the server, click New Connection..., otherwise select your database from the drop down box. You need to select whether or not you want to include your connection string information in your config file. This is up to you how secure you need your application to be. If you choose not to, you need to pass this information as parameters in code. Also at the bottom, you will most likely want to change the auto-generated entities name to something easier for you to use. This name will be the name of your DBContext Class!
Next you want to select the tables you would like EF to create POCO's for. If you want all the tables just tick the tables items. Same goes for views and stored procs.
Once you click finish, then you should see all your fancy POCOS laid out in front of you in Design Mode. Note If you do not have primary keys in place for all your tables, I suggest you fix that! VS will spit at you if you do not have Primary Keys set, or if your naming conventions in your tables prevent VS from creating them for you implicitly.
This is a quick little tutorial on getting you up and running. You can now persist and pull data from your POCOS by using your DBContext object. For example if you named your DBContext class *Gary_Bettman_Sucks*, and you wanted to create a new record from your table called NHL you would do the following:
Dim context as New Gary_Bettman_Sucks
Dim PullMyGoalie as New NHL With {
.Goals = 0,
.Playoffs = False
}
context.Set(Of NHL).Add(PullMyGoalie)
context.SaveChanges()
I would Highly suggest you look into implementing the Repository Pattern with your design to encourage code re-use, and it will seriously make your life MUCH easier down the road.
Hope this helps!

Resources