How to programatically create a database from an ADO.Net Entity Model? - c#

I am using .Net Framework 4.0. I have an entity model (.edmx) and I have a menu option in my program for connecting to an existing database or creating a new one. When the user selects to create a new database, I want to generate the database and schema from the entity model. Any idea how to do so?

Don't do it. It will only work on your v1 products and as soon as you'll plan to release v1.1 you'll realize you have no way of upgrading existing deployed databases to the new schema.
Instead, use scripts to create all your objects (including indexes, relations, constraints, everything) and use upgrade scripts to control how to upgrade each schema version to the next version, like described in Version Control and your Database.
A weaker alternative is to have a Visual Studio Database Project in your solution because VSDB projects have upgrade capabilities, although diff based. They allow you to deploy .dbschema files using the vsdbcmd tool and this tool can diff and upgrade an existing schema. You can have the VSDB project output be transformed into the EF model via an build transformation step (is not trivial, but is possible).
Overall, do not fall for the lure of the EF (or Linq) modeling as being the master definition of your schema. You'll pay the bigger price later.

Take a look there : How to: Generate a Database from a Conceptual Model (Entity Data Model Tools)

Related

Entity Framework Core Database First

My team has inherited a database application that contains hundreds of tables. The application uses Entity Framework and takes a database first approach for development. Our current process is to pull a table or two at a time into the edmx using the Update Model From Database... tool.
We are considering making a new API with .Net Core, but as far as I can tell from the research I have done, there is no equivalent process in the Entity Framework Core tools. The closest thing I can find is to reverse engineer the entire database with Scaffold-DbContext, and then use migrations for all future database changes. We can't scaffold the entire database, because some of the tables have errors, and fixing all those errors is not a viable option for us right now.
I have found that I can supply a list of tables that I want scaffolded with the initial Scaffold-DbContext call, but I'm not sure if migrations can be used in a similar way to the Update Model From Database... tool. Can I use migrations to add tables that already exist in our database? If not, what other options should I be looking at?

Migrating data from 2 different SQL Server databases using console app

I have 2 web applications which are let's say Legacy1 and New1. I have separate DbContext for both applications and separate databases.
Now I want to migrate data from the Legacy1 database to the New1 database. I cannot simply use copy database or export database options in SQL Server. Entities are different and I need to incorporate some logic while migrating data.
I have decided to use .Net Core Console (.Net Framework) project to do this. Do I have to add both DbContexts to this project? Also all the entities which I want to map and do the migration or is there any other way to achieve this.
Finally i found a simple approach to achieve that. I used EF DB First approach to generate edmx files for source and target databases. Which gives me access to corresponding Entities as well.
I Queried source database using source dbcontext and manipulated data as per the requirements and inserted into target database using target dbcontext. I used AutoMapper to map source entities to Target entities.
It was much simpler than i thought earlier. Hope it helps others having similar scenario.

Which Entity Framework technology am I really using?

This sounds a bit silly but I am uncertain of what it is I am actually using when I think of "Entity Framework". I noticed that when searching for documentation, I often end up with object and methods that I simply do not have or use directly (such as objectcontext, EntityState, ect). I'm also unsure of which Linq I'm using at any given time (Linq2SQL, Linq2Entities, Linq2Objects).
What I think I'm using:
Database-first ADO.NET Entity Data Model and Linq to Entities.
My setup:
Microsoft Visual Studio Professional 2015, installed with default settings using the installer downloaded from the official website. Up to date, no additional addons, library or packaged installed.
What I do:
Design a database like this and create it in SQL Server
MyFooDB
table Foo ( PK int Id, varchar Name)
table Bar ( PK int Id, bit Active, FK int FooId references Foo.Id )
Open VS2015, File > New > Project > C# > Windows >Console Application.
In the solution explorer, right-click the project node > add > new item > Data > ADO.NET Entity data model.
A dialog opens, I choose EF designer from Database, connect to my server and choose my database "MyFooDB". At the bottom of the dialog it says that the setting will be saved in the app.config as "MyFooDBEntities".
I get asked which version of Entity Framework I want to use, this time I'll choose 6.X .
A dialog appears for me to choose which database objects I want to include. The model namespace text field says "MyFooDBModel". I check "Foo" and "Bar" then click Finish.
An edmx file is created and opened. I see a diagram of the objects I chose. Under the edmx file I see this structure:
MyFooDBModel.edmx
-MyFooDBModel.Context.tt
-MyFooDBModel.Context.cs
-MyFooDBModel.Designer.cs
-MyFooDBModel.edmx.diagram
-MyFooDBModel.tt
-Foo.cs
-Bar.cs
In Program.cs, I instanciate a MyFooDBEntities and use it:
var db = new MyFooDBEntities();
var firstFoo = db.Foos.First(x => x.Id == 1);
if (!firstFoo.Bars.Any())
{
Bar b = new Bar() { Id = 3, Active=true };
firstFoo.Bars.Add(b);
db.SaveChanges();
}
And so my question is what are the different technologies I used and how do they relate to each other?
LINQ2SQL is a completely different (and no longer supported) technology, it used to have different templates and was sort of the "quicker and simpler" version of EF (although there was no actual relationship between the two).
LINQ2Objects is the description of applying LINQ expressions to objects/instances in memory, not really the sames as either EF or L2S.
You are indeed using Entity Framework, along with the Entity Data Models (EDMX). EDMX is a carry-over from older versions of EF where you had a design surface (remember when those were cool) for designing your data model. With EF6.x, you can still use these and they are essentially used to generate .cs files for you. This is no longer what most people would consider to be the "Best Practice" for using EF, the preferred way of generating your model now is using Code First and POCOs.
Under the hood, EF 6.x still has a lot of legacy dependency on the EDMX paradigm and so the code first and fluent configurations are used to build this up to some extent, so I don't think there is any significant functional differences between the two. But generally speaking code first is much easier to work with, maintain, and for 3rd parties to understand your code.
With Entity Framework Core (formerly Entity Framework 7), it's been completely re-written and the EDMX models are no longer supported, so for that reason alone you might consider ditching them (not sure if or what the migration path is between 6 and Core).

Easiest way to set up a file based database with Entity Framework

I'm developing an application, in part to learn new technologies - in this instance Entity Framework.
I'm using .NET 4.5, Visual Studio 2013. I need some way to store data that doesn't involve running an SQL server on my tired old laptop (not even the lightweight one) and works with Entity Framework. Ideally I would like something that just stores data in a file. I have a few basic requirements:
A simple relational database. Max 10 tables, this is a prototype, so
the tables won't get very big
Ability to design the database in some
sort of GUI tool
Ability to generate Entity Framework database model
from the DB. I don't want to type those in manually.
No proprietary software other than what I'm already using - i.e. I don't own a MS Access license
Something that ACTUALLY
WORKS
I've spent the better part of today trying to get VS2013 to connect me to an SQLite database that I've created. I never got anywhere. SQLite is simply not listed among the available types of data sources. I've read StackOverflow posts about this issue, I'm done trying to make it work. (this is where the frustration you can sense in this post stems from - this is SO not the point of the exercise, yet I've wasted so much time with it)
Is there any other file based database technology I could use?
Your needs are not compatible, the best choice is SQLite but you can not generate model from database and must use EF as CodeFirst .
and your problem with SQLite can easily fixed by using correct provider and configuration : http://www.bricelam.net/2012/10/entity-framework-on-sqlite.html
I suggest you use SQL Server Compact in combination with my free SQL Server Compact Toolbox VS extension:
A simple relational database. Max 10 tables, this is a prototype, so the tables won't get very big
SQL CE supports all standard relational paradigms, and has a max database size of 4 GB
Ability to design the database in some sort of GUI tool
The extension allows you to do this in Visual Studio
Ability to generate Entity Framework database model from the DB. I don't want to type those in manually.
Yes, fully supported
No proprietary software other than what I'm already using - i.e. I don't own a MS Access license
SQL Compact is free and freely distributable

Mapping and sql database creation

I started a project and use Entity Framework 5.
Now I created a database on my SQL Server Express with all tables.
Further I created the DbContext with a fluent mapping.
What is better, the fluent mappings or the .edmx mapping files?
The database is now on the SQL Server but I want support also SQL local db.
Is there a way to tell the EF that I want to use a SQL local db?
Should I ship the whole database within the setup or better to create the database on startup of my application? How can I use EF to create the database (SQL server or SQL local db)?
Every useful help would be appreciated.
If you are using EF5 I would stay away from edmx.
You could reverse engineer your model from database using Entity Framework Power Tools.
Than you can customize your model using either Data Annotations or fluent mappings.
If you use EF5 code first it can create database automatically for you if not present, however that would not work very well on subsequent upgrades (it can recreate database but then you will use or your existing data). The options you could use, is either EF migrations, where you can specify in fluent-like languagage the modifications that were made to your database or use Database Project in Visual Studio, where you can store all your schema in source control and then generate database upgrade scripts betweeen releases.

Categories

Resources