Entity Framework: How to update database when modifying Model - c#

In Entity Framework 4 there are the options "Update Model from Database" and "Generate Database from Model". But what I'm missing is an option like
Update Database from Model
which reflects the changes made in the model (e.g. adding a new Property or Navigation-Property) by modifying the database schema (e.g. adding a new column). without losing its content.
Does someone know a way to achieve this or is there a t4 template that can perform a schema update without dropping existing tables? (I'm using Visual Studio 2010, .Net 4.0 and SQL Server 2008)
Thanks

Best way that I've found so far is to update the database first, for example adding a column to a table and then update the model from the database. This works well for small changes.

Related

EF Core DB first - Do I need the index definitions?

I am using EF Core (3.1 if that matters) with a DB first approach.
A while ago I generated the model using the Scaffold-DbContext command, and in the generated context file there are many index definitions, e.g.:
entity.HasIndex(e => new { e.ItemType, e.ItemID });
Since we had some index updates recently I was wondering - Do I actually need to update all the relevant index definitions in my model? Do I even need them at all? Do they have any influence on EF's performance or how the code runs, or can I just remove them and not worry about any updates made in the actual DB?
Thanks in advance
In the Database-First approach unlike Code-First, there is an existing database with its own configurations that may change in the future.
By this I mean, you always update your model based on the database objects and changes done in the database.
In your case, I suggest you use Database Project in visual studio for your database to make it under control by using Source Control. The following article might help you:
Create Your First Visual Studio Database Project
To my mind, it is not important to keep the database changes in the same application project. Keep all database objects in another database project and only use the database model for your application model. You can build it and then deploy the database project on SQL Server.
Indexes do not directly affect EF functionality, but the database performance affects its output. "An index is an on-disk structure associated with a table or view that speeds retrieval of rows from the table or view."
Clustered and nonclustered indexes described

Class does not accept new fields after updating model.

I am developing using VS2012, SQL Server 2008 and ASP NET MVC
I have a ADO.NET Data Model created from a SQL DB and every time I change the DB I know that I need to update the model. Using VS2012 I click in update model and I can see that the entity is updated.
the problem is that if I try to use a property from a context after update the model I still can´t use the new fields.
I am having to delete the model and recreat it again.
I am not sure I am missing something or this is the normal procedure.
Usually you have to rebuild your solution to have the properties show up in IntelliSense.

Adding a new table to EF

We are upgrading an old VB6 application which sits on a SQL Server 2005 database, to an Entity Framework solution. The database remains the same, except - we're adding a new table. Is it possible with Entity Framework, to maintain the existing structure, when it gets installed on a client PC - and just add one new table?
Is this how Code First will work? Can I be 100% certain that no other tables will be modified?
i don't think, the effort is worth it to switch to code-first if you have an existing database and want to add only one table.
it is possible to map code-first classes to an existing database (reverse engineer code first). actually, i'm not very experienced with that workflow, but i know you can. You have to deal with a lot of manual mapping (with DataAnnotations or Fluent API), so in your case i would recommend to use the Entity Framework Database First workflow, since adding a single table saves you a lot of work.
this link has some useful information: Arthur Vickers Blog - Don't use Code first by mistake
You have two options, use a database editor such as SQL management studio to create the table which you can then map to a ef entity, or use migrations for ef which will let you update your database via ef.
Take a look at the migrations tutorial here: http://msdn.microsoft.com/en-gb/data/jj591621
I am using the database first approach, since a database developing team is doing the changes I require in the database on the SQL server for me.
Hence, I have to update the EDMX whenever the schema in the database changes.
Note: Changing one single table directly does not work for me, because VS doesn't always detect the changes right (for this issue, here are some details in SO if you're interested).
Hence, I am using the following workaround (regenerating all the tables):
In VS 2012, open the EDMX file by double-clicking on it. The graphic representation of the tables is shown.
Left-Click into the EDMX designer, then select all tables by pressing CTRL+A. Then, remove them by pressing DEL.
Right-Click into the EDMX designer and select "Update Model from Database ..." in the context menu.
The Update Wizard opens. In the "Add" tab, check "Tables", and depending on the requirements, check "Pluralize or singularize generated object names", "Include foreign key columns in the model" and optionally "Import selected stored procedures and functions into the entity model". Usually, I am using the "Pluralize..." and "Include foreign key columns..." options.
Click Finish. Now Save by pressing Ctrl+S.
That workaround works fine for me, and requires just a minute to update the model reliably.

Keep Database Content On Model Change

Using the code-first approach available in the new 4.1 RC.
Is there any way to persist the current data stored in a database when the mode changes? The database is created by the entity framework, and usually the database is dropped and recreated on model changes.
Obviously as soon as the model is changed it will not be possible to use the context object to connect to the database to retrieve the data, so what are the options?
Code first doesn't support database migration / evolution yet. If you want to do incremental DB development use model first (EDMX) with DbContext Generator T4 template and Entity designer database generation pack which is able to create diff. scripts from the model.
From Scott Gu:
Importantly, though, the auto-create
database option is just an option – it
is definitely not required. If you
point your connection-string at an
existing database then EF “code first”
will not try and create one
automatically. The auto-recreate
option also won’t be enabled unless
you explicitly want EF to do this – so
you don’t need to worry about it
dropping and recreating your database
unless you’ve explicitly indicated you
want it to do so.

About model first EF4 development

I just tried that out, and I also found that If I changed EF model and want to regenerate the database from model , all existing data in database will lost.
Is there any way to fix that, as this always happens during the development.
Check out Entity Designer Database generation powerpack
it's a visual studio add on from microsoft, which let's you modify your db schema without wiping your data.
James Gaunt basically nailed it - generate the schema in a new database and use schema update tools. You don't necessarily need to buy anything there are schema and data compare and migrate tools in Visual Studio under the "Data" Menu.
Try Devart Entity Developer. We offer the database synchronization functionality for Entity Framework and LINQ to SQL models, see this article for details.

Categories