Most efficient way to delete database data in Entity Framework 4? - entity-framework-4

For integration testing I need to wipe all data information in a database.
I'm using Entity Framework 4
What's the most efficient way?
Would be nice if I don't need to specify table or class names every time I add a new class to the model.
Thanks!

Most efficient way to delete database data is to drop all tables and recreate all tables.

This may not be the fastest, but it is the simplest. It even clears your connection pool:
using (var context = new DataContext())
{
context.Database.Delete();
}

Related

Core data versioning/migration after deleting entities

I have a database which consists of 3 entities, say
'IronMan', 'CaptainAmerica' and 'SpiderMan'.
With new changes, I want to delete all three entities and create another entity called 'Thanos'.
I would not need to use any of the code and data stored earlier with entities 'IronMan', 'CaptainAmerica' and 'SpiderMan'. Do I need to do core data versioning or migration in this case?
As mentioned in Apple's documentation
https://developer.apple.com/documentation/coredata/using_lightweight_migration
You can add, remove, and rename entities in the hierarchy.
So, yes, you need to provide migration (new version model) but can stick to a light-weight migration, hence it will be done automatically based on changes done between the 2 models.

How avoid Entity Framework 6 populating new databases?

Is there a native Entity Framework way to create a database (LocalDB) without populating it with the tables defined in my DbContext class (code first)? (I want to use my own database creation script, with default values, etc, and I want to be able to clear it out beforehand using context.Database.Delete().)
If I use context.Database.Create() then EF will populate the database with tables. If not then no database exists and I cannot connect to it to run my database creation SQL.
I am providing a connection string like this:
data source=(LocalDB)\\MSSQLLocalDB;integrated security=True;attachdbfilename=|DataDirectory|\\FileName.mdf;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework
Create a base DbContext instance that uses the same database, and use it to do the recreation.
var my_context = new MyDbContext(connection_string);
var creater_context = new DbContext(connection_string);
creater_context.Database.Delete();
creater_context.Database.Create();
// now use my_context on the database.

Entity Framework Database First Approach

I am working on a project in which I am assigned to implement database first Approach. Here, I want to know that when we initiate database first approach we map that to an existing DB, but what if I have another DB with the same structure but different data, can I use that DB by just changing the connection string ? or will it impact somehow?
It will work when you change the connection string. I recommend you select the 'Code First from database' when creating new 'ADO.NET Entity Data Model' with VS add new item.

ZF2 return inserted row

I know I can get the just inserted row id with
$this->tableGateway->getLastInsertValue();
I was wondering whether it is possible to return the just inserted row as object?
Or do I have to make a query to the database with the new id?
Is this fine or against development best practises?
Thx I really appreciate your help and your experiences
To answer your questions:
Yes you should query the database with the new id if you know that the data you inserted is in a way modified or different than what you inserted (for example if you let the database insert a timestamp or whatsoever to the record upon insertion).
I personally don't think it is against best practices, however you should try and make everything as streamlined as possible. I don't know how your code is set up, but try to compartmentalise as much as you can so that you can reuse most of your code.
Personally I would use an ORM like Doctrine instead of the Zend Framework 2 database features, but that all depends on what you need from it really

Can Entity Framework be used to partially map an existing database?

Just getting started with EF v6 and trying to see if I can retrofit it to an existing database. In some ways it looks like the existing database will map well to an object model (HealthProviders, Patients, Visits) however in other ways some of the tables won't map easily to objects. That being said does EF require mapping all database objects to code or can you pick and choose which database objects are modeled in EF?
Yes, you can. Using database first - the default - you get to choose which objects - tables, views, stored procedures - you want to map.

Resources