Is database recovery from EF model possible? - c#

I had a web service project, with a database integration of course :) I use Entity Framework, so I generated my EF model from my db (sql server express). Everything was okay till my computer got broke. I had my project files backed up so I recovered them. But I didn't have the db backup(shame).
As far as I know there is this concept Code First, and what I am wondering is can I use it to regenerate my db? Otherwise I'll have to try getting the db from the old disk, or rewriting the whole db.
Before going into those struggles I wanted ask and get a proper answer to this since I thought it could help others too.

Of course, you can.
Specify a connection string in your DbContext constructor, which points to a non-existent database, and the database will be created from scratch.
The EF workflows are "reversible" in the sense that you can go from DB to model or from model to DB, no matter if the model is a graphic EDM or a DbContext.

Related

How do I handle database-first when the database doesn't have any links (and I can't add any)?

I have been asked to write a web site that will use an existing SQL Server database. The database was designed to work with another application, and so I can't make any potentially breaking changes to it.
Unfortunately, the database does not contain a single relational link, each table is standalone.
I know you can use EF commands to scaffold a database and create entity classes, but I would like the code to know the relationships that should exist between the tables.
I thought about scaffolding the database, then modifying the created classes to include the links, but I'm not sure if that would allow EF to load related entities. Also, as I will need to add some new tables to the database, I'm worried that EF will try and create those links when I do the migration.
Is there any way to do this?

Migrate EF6 database-first from SQL Server to PostgreSQL

I am looking for easy way to reuse edmx file created with EF6 over SQL Server to PostgreSQL
I have used database-first with EF6 on SQL Server and everything worked fine.
We are going to use PostgreSQL.
I have already migrated the database to PostgreSQL, and installed EntityFramework6.Npgsql, and I wanted use database-first approach again.
I tried to update from database but it looks that I have to fix all the edmx file. The original entity model is quite complex, with abstract class, complex types and enum types.
The new edmx file lost inheritance and complex types.
Do you know an easy way to do it?
I don't know if you can get the EDMX file to work with PostgreSQL, but you might be able to use scaffolding and get a db first approach. Something like:
dotnet ef dbcontext scaffold
This would allow you to make changes in the database and have them reflected in code. I'm no PostgreSql expert but this article sure makes it look like it's possible.
If you want a more graphical approach then you may want to consider switching to code first and using the class designer in visual studio.

EF Core Database First

We are using EF Core with database which constantly changes by database administrators. So we can't always check changes in this database. What is the best approach to use EF Core with such database?
We tried to use Database First approach but we can't always regenerate model classes in project.
Dit down with management and talk about it. I see no way to work when the db constantly changes.
The way we do it here now is that special people are responsibel for:
Generating changescripts that automatically are applicable
Making sure the EF model is kept in sync.
When neeed we have the ability to give every developer his own environment AND have environments for valiation.
There is NO Way to do any work when you work against a database that 3rd parties are constantly changing, and it gets worse if you are more people.
One way you can track changes in your database is by doing source control for it. At least, then you know what changed. Still not the best scenario overall when someone else modifies database with poor communication.

How To View Database Diagram?

I am working on a project which requires me to write several POCO classes using Entity Framework Code First. There is a lot of entity relationships and inheritance going on and its hard to keep track of everything just looking at the code. Now, as we know, Entity Framework Code First yields an .mdf file as your database, and i was thinking for verification, a database diagram would server me better.
Is it possible for me to view my database diagram in this scenario, and how may i do so??
You could always point it to a SQL Server Express database - by default an MVC 4 project uses LocalDB but if you're more comfortable in management studio you can always create your own database and change the connection string to that.
Also from memory I believe you can also attach an mdf file in management studio but may have trouble while the application is running. But I could be thinking of something else there.

Entity Framework Code First on external SQL server

I'm following the Entity Framework tutorial on:
Link
I've downloaded the source code, and ran. The project works fine (using the default connection string).
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0" />
Next i've changed the connection string to connect to a remote server (which successfully connects). However the tables aren't created and when running the application and accessing the controller I get the following error.
Error:
Model compatibility cannot be checked because the database does not contain
model metadata. Model compatibility can only be checked for databases created
using Code First or Code First Migrations.
My database user is 'dbowner' so I wouldn't imagine it's database access issues.
I'm new to EF, and don't know much about Code First Migrations. Have you come across the above error, and would Code Migrations solve this issue? If so why?
From my reading (please correct me if I am wrong) the scenario here is that you have an existing (perhaps empty) database on a remote server that you wish to put your EF code-first work into.
The error is coming about because, I think, EF is looking for a _MigrationHistory table (metadata about what EF code-first migrations have been run against it etc) and can't find it. There is some reasonable coverage of this table here for some background/interest reading.
So to resolve the issue, I think the steps to take are:
Initialise your database so that it acknowledges the EF code-first stuff
Update the database, rolling forward all your migrations to date
I found some good coverage of how to do this here. This blog also has some good coverage of EF topics in general
PS. I am guessing that your .dsf/SQL Compact Edition database wouldn't have had this problem because EF code-first would have created it for you on first run so it was already acknowledged as being a code-first database.
Here is a link to Entity Framework Power Tools. It is made for creating models by 'Reverse Engineering' your Database on a remote server. You can then easily access this database using EF.
Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database
Both of the initializer methods which I had tried fail when the database already exists:
Database.SetInitializer<Context>(new Initializer());
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Context>());
However it is possible to force the database to be dropped using:
Database.SetInitializer(new DropCreateDatabaseAlways<Context>());
The following SO post provides the answer to my question:
Setting up a Entity Framework Code First Database on SQL Server 2008
I've tried a combination of the two, and have decided that the best solution is to manually go into SQL Management studio and DROP the database, and re-create it using the initializer as this allows me to Seed the contents of the database.
Database.SetInitializer<Context>(new Initializer());
See here for more information on Seeding the database as it is also quite an unstable processess!
How can I get my database to seed using Entity Framework CodeFirst?

Categories

Resources