About model first EF4 development - c#

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.

Related

How to update SQL Table structure from Entity Framework Model

I am new to Entity Framework. I have created an EF Model and successfully added some tables and relation. Then I Clicked Generate Database from Model and My DB has been updated. Then I renamed some columns and I don't know how to revert or apply the changes. And Update Model from database does not seems to work because the columns names are different yet.
I need to graphically sync DB with Model. I prefer the model data rather than db data.
Thanks in advance.
You might want to look into the Code-First approach of Entity Framework. Using that approach you'll define your model in your code, and when changing anything you can create a Migration which allows you to up- and down-grade the DB to a specific version from the package manager console (or just create the respective SQL scripts).
For more information on this subject please see this article on MSDN
Note that you can also reverse engineer the code first model from an existing database (see 3. Reverse Engineer Model in this MSDN article), and then enable migrations for that model (see Step 2: Enable Migrations in this MSDN article)
What I do when I make "updates" is do it on both sides manually, in db and then in model (by right click properties) if the change is small. If adding a "new" table I drag it over to model from db server connections panel.
The alternative I've seen others prefer to use in this cases is to stay away from Entity Framework and use Dapper where you pass queries to it and it handles the rest.
Dapper (Wins!) vs Entity Framework vs ADO.NET Performance Benchmarking

How to add Entity Framework Geolocation property in Design Database model

How can I make an Database Model edmx file that would let me add GeoLocation column to it from design database that would let me generate / reverse engeneering from database.
This one implies that I would modify it by hand.
Do I need to install a plugin for visual studio ? Do I need to find an open source entity generation tool that would help me?
p.s. i am using thisADO.net Poco Generator
I use SQL server 2008 and .net framework 4.0
p.s. 2 What about this reference
p.s. 3 Here on MSDN are some facts about not supporting this feature http://social.msdn.microsoft.com/Forums/en-AU/sqlspatial/thread/34866400-91e5-4af5-9fee-f7f369aa6a61
As far as I now the geolocation support in the Entity Framework will be released with .NET 4.5, so until then you will have to do it manually.
The second reference you provide should help you out. What the author describes is a way to store the data in the database, make sure it stays up to date with some triggers and then in the partial class in your code define a property that will convert the data field from your database to actual LatLong data.

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.

Using Linq to SQL and dbml to create _and_ upgrade database?

Im using dbml (c# vs2010) to model my database and DataContext.CreateDatabase() to create it. Some time later I add a new property to one entity-type and now I want to upgrade the SQL-database to fit the new version of the dbml-schema. How do I accomplish this? Do I have to delete and re-create the database or upgrade it manually? Is ADO.NET EF better in this respect?
Update:
When searching for methods of updating the database according to changes in dbml I only get results for the inverse; updating dbml from changes in the database. But what is then the visual designer in dbml for? I want some master-design-view where I can do my changes and then generate sql-upgrade-script from that. Isnt that what everyone wants?
Neither Linq2Sql nor EF are made for this kind of scenario.
You asked if this is possible so in response to that I'll say that in theory I guess you could hack around this in EF by altering the schema of your db and using some script to alter the DBML file but I would highly recommend against it. Use the right tool for the job.
Sounds like ADO.NET might be a better solution for you.

Entity Framework: How to update database when modifying Model

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.

Categories