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.
Related
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.
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).
I want to create an Entity Framework Model based on database using VS 2015.
There is some problem:
1) When I want to create model I get this warning:
2) When I create mode some file being generate like T4.
3) Generated class hasn't Data Annotations like :
[EdmEntityTypeAttribute(NamespaceName="SomeModel", Name="tblCode1")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
4) In our group there are some people that have VS 2013 or 2010 that they can't use this type of models.
How I can create model using VS 2015 like VS 2010 or 2013 without T4 (Just 2 files like below)
Short answer:
You need to change the Code Generation Strategy property of the edmx from "T4" to "Legacy ObjectContext" and delete the .tt nodes from the Solution Exporer.
Long answer:
Here are the steps needed:
(1) Assuming you start by selecting "Add -> New Item -> ADO.NET Entity Data Model -> EF Designer from database".
(2) Follow the "Entity Data Model Wizard". The next is very important. When you get to the screen which asks you for the EF version you want to use, make sure you select EF 5.0, otherwise T4 template will be the only option you have:
(3) Finish the wizard. Say OK to security warnings like your first screenshot. The project structure will look like your second screenshot.
(4) Open the edmx file in a designer. Look at the Properties Window, there is a property called Code Generation Strategy which by default is "T4"
Change the property to "Legacy ObjectContext"
(5) Delete YourModel.Context.tt and YourMdel.tt nodes from the Solution Explorer and you are done.
A bit boring, but does what you want. The drawback is that you are limited to an outdated EF version, and the upcoming EF7 will retire edmx at all, so it might be a good time to start thinking of switching to the Code First approach. But until then, hope the above will help to keep your current process.
For generate "Code first" model (obtain a code first model starting from database) you can also use Code Smith Generator. You can start from POCO classes templates (or from NHibernate templates if you need serialization, IDataErroInfo, INotifyPropertyChanged, and so on implemented) and customize them is really simple (the syntax is based on ASP). Probably there is also a free version.
Looking around I found also an article and an open source tool that extract POCO classes from SQL Server database.
http://www.codeproject.com/Articles/892233/POCO-Generator
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
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).