I can update database table from edmx model? - c#

Is possible update database table from edmx model, for example if I create a new field in edmx diagram to a table, I want update corresponding mapped table in database adding this new field ... in visual studio menu I find the voice "update model from database" and "create database from model" , but I not find "update database from model"...

In the EF EDMX "Model-First" workflow you always generate a full database DDL script, and for incremental changes use a schema-compare tool like SSDT to create the change script.
See eg https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/workflows/model-first#5-dealing-with-model-changes
It's not a very popular or useful workflow, and the tooling for incremental updates was just never built. The vast majority of EDMX users do a database-first workflow with no customization of the EDMX.
But you should really stop using EDMX. The graphical designer seems simpler to begin with, but using code-based mapping is simpler in the long run.

I don't believe Edmx (database fist) works that way, at least, out of the box. You need to use code first, and enable migrations.
Edmx is nothing more than mapping to your data, that doesn't tie into migrations.
I believe there is a way to auto-generate your code first as well using visual studio...just like the Edmx.

Related

Adding an entity (and respective table) to EF DB-first model

I'm pretty new to Entity Framework: I started from a database-first model to maintain an application created using a strange mixture of EF and plain old SQL.
I created my own fresh DB-first model and I'm fine with it. Today my boss asked me to add a new entity. Lack of foreign keys simplifies the scenario.
I have created my new entity in the diagram (it's made of three instances of a Complex Entity I just created) but now I have to make an incremental DB script to create the new table. I'm supposed to do that both for MySQL and SQL Server but let's start with the second.
So now I see that I have a compilation problem "No mapping for entity Entity" and if I use "Update model from database" command I see no option for pushing changes to DB, but that sounds correct given the word "from".
OK, I have tried to click "Table Mapping" from the right-click menu and I found the option to map the entity to the table. I was going to type the new table name in the "Add table or view" field and... WAIT! I can only select existing tables
I understand it's just for a single table so I can simply "Generate database from model" in order to get the full SQL script, find the table I want, run that to DB and "Update model from DB" so EF will see the table, BUT
I would like to understand how to create incremental scripts with Entity Framework. That is my question.
You indicate you have a database-first design but appear to be working from a code-first mindset.
In database-first design the entity model is subordinate the underlying datastore. Changes to the model (or at least changes to the model which also require changes to the underlying datastore) occur FIRST on the database.
So how do you create a new table for the entity? You create the new table in your database (CREATE TABLE ...). Then using the "Update Model From Database" wizard you select the new table from the "Add" tab. EF will create the corresponding EF class automatically. If you already manually created the entity you should delete it otherwise you could end up with some weird entity naming (i.e. Customer1).
Database first does not have the capability to support table creation at the entity layer. Changes to the database are always one way, from the database to the entity model, hence the term "database first".
On the other hand if you are more comfortable creating entities directly and want to build a database from a set of entities you should be looking to create a "Code-First" design. Despite the name "code first" it is possible to get an initial set of entity classes from an existing database. The term "code first" refers to the origination of changes to the db/model structure.

Regenerate Entity Framework Code First Models

Using Entity Framework 6 Code First in an ASP.NET project with Visual Studio 2013, is there any way to rebuild (or update) the generated DataContext and model classes without stepping through the Entity Data Model Wizard every time?
I'm fully aware of how to do this with an EDMX designer, but again, I'm using the "Code First from database" method and just wondering if there's a one-click (or one console command) way to trigger the rebuild without having to delete the generated context class and then step through the Entity Data Model Wizard every time I make a change to the backing database.
In VS 2015 (and supposedly 2013/2012) you can use the Entity Framework Reverse POCO generator to accomplish this.
https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838
You can make all your changes to database first, and to re-generate your models all you have to do is save your Database.tt file (usually I just add white space).
Code first requires you to create the DataContext by hand. You don't create/change the database and refresh the DataContext class. The Code first from database or EF Reverse POCO template is a middle ground between true Code First and the Database First approach of doing things. It meets at the middle by generating the same kind of POCO classes that you would have written by hand in Code First. They don't do it in Code First way but the end result is something similar to Code First. Hence the confusing name.
If you are using one of these templates to generate POCOs, you can right click on the t4 template file and click Run Custom Tool. If that doesn't work, you might want to delete that entity and then run the custom tool again. Also right clicking on the EDMX and clicking 'Update Model from Database' should work.
I have the solution for rebuild without wizard:
using the t4 is Transform All T4 Templates
and create the classes

EF Database first how to update model for database changes?

In a class library Ado.net Entity Data Model is has generated POCO classes. These were generated fine for the first time. But database changes are not being reflected. In edmx diagram right clicking and choosing Update Model from Database show newly created table but it do not add table even after selecting it to add.
I tried running .tt (by right click and Run custom tool) but even it did not regenerated the Poco classes as per latest DB changes.
Help please
Not a fix but a workaround: Is it not an option to simply remove and regenerate the EDMX and the generated classes? That's what I do, it is much easier than working with the update feature, and the result seems to be the same. Your POCO extensions still remain the same and functional.
I use database first and I have my SQL upgrade scripts, the generated EDMX and my Generated models in source control and the changes there are very easy to manage. Here is a rough outline of my DB upgrade process for each version:
Create .sql script for the upgrade, statements like CREATE TABLE etc.
Delete generated files: Model.Context.tt, Model.tt, Model.edmx
Remove Entities string from Web.config (if you use it)
Create the EDMX and Context files the same way you did for the first time
If you use source control (I hope you do!) check what has changed
Test
Commit!
In my case i needed to save ModelName.edmx, then classes were generated.
Ensure that connections string in app.config is correct. I was using a DataDictionary and my connection string had the following path:
data source=|DataDirectory|*.sqlite
Thus, it wasn't updating. Because this DataDirectory variable was being resolved at runtime.

EF 6, update model from database does not support object context?

We have a fairly elaborate data model built from an existing database, then enhanced using partial classes to support additional methods & properties, inheritance, etc. We have not yet bitten the bullet to update this from ObjectContext to DbContext.
I am using VS2012, .net 4.5, EF 6.0.2, and have installed the EF 6.0.2 Tools for VS2012.
Following the recommended mechanism to update a project to EF6 (http://msdn.microsoft.com/en-us/data/dn469466) has been successful, including the addition of the EF 6 Entity Object generator as the code generation item.
Subsequent to making this change, I would like to update the model from the database to incorporate some recent schema changes into the model. Running through the "update model from database" dialog, what appears to be happening is that an entirely new dbcontext-based model & template is added to the project, in addition to the object context-based model that already exists in the project, and none of the changes are incorporated into the entity-object template.
Of course, this means there are hundreds of duplicate names defined in the project once the operation completes. The edmx properties do not appear to have an option that would control this behavior.
Certainly, making the transition to dbcontext is the right avenue ultimately, but would like to avoid taking that on imminently.
My experience is open edmx file in visual studio by double-click and then select all tables perform a full delete(press del). Then click save button on top of menu bar, it should delete all models automatically.
After it's done, then you can update model from database again.
Hope this helps.

Making changes to existing entities in Entity Framework

Suppose I have an existing database set up using Entity Framework. Is there a mechanism through which I can safely add or remove entities (or their properties) such that the database is altered automatically?
I know there's an option to "Update Model From Database". Is there an equivalent "Update Database From Model" ? Is there a way to configure Visual Studio to do this automatically?
Entity Framework 4.3 has Code First Migration support.
EF helps you with checking the differences between your code and database and then generates code for you that handles this changes. You can use the NuGet package manager console to enable migrations, add a new migration and run them against your database (or create a sql script).
This blog explains how the Migrations work and this blog shows how you can use it with an existing database
Altering the database schema isn't a straightforward operation (has a column been renamed, or is it new column? Can the old type be converted to the new type?) that you can easily infer from the model.
EF doesn't alter the tables for you - it can Drop-Create the DB for you when you change it. However, if you change the existing database by hand to suit the model, EF doesn't seem to mind. It looks like what they check for is Hash(Model) = Hash(Tables).

Categories

Resources