EF Context missing a collection - entity-framework

I'm trying to generate an entity from my SQL database using the ADO.NET Entity Data Model using the ADO.NET DbContext Generator. When I generate my edmx from the database I can see it in the model. I right click on my tt file (which is in a separate project) and run the custom tool. The entity appears. It's called CustomerContact. However, my dbcontext does not have a CustomerContacts collection. What is going on here?

I figured it out. The problem was the different versions of Entity Framework. I updated both projects to the latest version of EF and it works now

Related

generate dbcontext objects from edmx with mappings

In our company we have a lot of legacy applications which use an edmx file together with EF4.
We would like to migrate to EF6 (or EF core). Is there a way that we can generate the code first objects (dbcontext, entities) from this edmx with the mappings.
How can we do this?
Maybe the best solution for this will be scaffold (reverse engineer your model) from current database.
EF Core has nice powershell tools for this scaffolding https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
You can also split the whole (maybe big) DbContext to more DbContexts with specified Table parameter
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
EF 6 has also some options for scaffold actual database Entity Framework - Generating Classes
Why would you bother generating anything from .edmx? What if you created new project with code first from existing database?

Difference between generate an entity data model in EF4 and EF6

Before I used EF4 to generate my entity data model from an existing database. I can do CRUD operations on every generated entity, because the entities context class with all the methods is automatically generated. Now I have upgraded my project to EF6, deleted the files created by the EF4 data model wizard and generated it again using EF6 data model wizard. Now I get a T4 template file with a context class below it, a T4 template file with for every entity a class with code, a empty designer file and an edmx diagram file. But there are no methods such as AddObject, DeleteObject, SaveChanges generated. How can they be generated as it did before with EF4?
EF6 should still generate a context for you. It provides the mentioned methods. Are you sure you just missed in amongs all the .tt files?
EDIT: Also, make sure you have all the proper references to EF. There might be a compiler warning indicating a problem.

Entity Framework 5 - How to generate POCO classes from existing database

I am using VS 2012 and EF 5. I have an existing database that I want to create POCO classes from the existing database. I followed the steps to add an ADO.NET Entity Data Model to my project. I went through the wizard to use an existing database. It then created the edmx and tt files with the designer open. However, I want to create the POCO objects and use them. The Microsoft site states that the POCO Entity Framework Generator is obsolete and I should use the DBContext Generator. I can't figure out steps I use to generate the POCO classes. I only see the edmx designer. I really don't even want an edmx file but instead just POCO classes. How can I get POCO classes to be created from an existing database using EF 5 and VS 2012?
Use EF 5.x DbContext Fluent Generator
You can add it from online templates:
Generate edmx from existing database
Select Add New Item
Search online templates for POCO
Add EF 5.x DbContext Fluent Generator
It will add three T4 templates to your project:
XXX.Context.tt - context inherited from DbContext
XXX.Entities.tt - POCO entities
XXX.Mappings.tt - fluent mappings for each entity
BUT you need to setup path to your edmx data model manually. Each of these templates have line string inputFile = #"$edmxInputFile$";. You need to provide name of your edmx file here:
string inputFile = #"Northwind.edmx";
The process to do this is pretty streamlined now, it seems. The steps from the accepted answer are now easy to do from the EDMX designer itself. Basically,
Generate the model (edmx) from an existing database by adding ADO.NET Entity Data Model to the project (see here for more details),
and then
Open the .edmx file in the Entity Designer.
Right-click an empty area on the Entity Designer surface and point to Add Code Generation Item.
In the Add New Item dialog, select Online Templates and type DBContext in the Search Online Templates text box.
Select the appropriate version for your template (5.0, if you want to target the Entity Framework 5.0).
Click OK.
This will do all the work, apparently. The quoted instructions here refer to Online Templates as installing EF 5.x DbContext Fluent Generator is required. If you have it already installed, there is no need to search for it in the Online Templates but in the Installed Templates.
For more info you can check this page, section "To use the DbContext Generator Template to Generate Object Layer Code".

ADO.Net Entity Model (edmx) vs Entity Framework(v4.0 etc)

What are the benefits of using ADO.Net Entity Model and EF?
Can we use both of them together in a project. I came across an example where, the user had used both edmx and ef for his application. I am not sure what is the purpose of that.
Thanks
Edmx artifact (either in the form of a file on the disk for Model First and Database First approaches or being generated by the EF runtime - Code First approach) describes your model, your database and the mapping between these. At the moment EF always needs it to work. The only nuance is that for CodeFirst applications (or, in general, applications using DbContext) this file is generated on the fly from your classes and you don't deal with it directly while in case of Model First/Database First where you use the ObjectContext the file is on your disk and (usually) is split and embedded in your assembly.
EDIT
EF6 no longer creates and uses artifacts internally (at least for CSDL and SSDL parts). However you can still dump the model in form of EDMX using EdmxWriter.WriteEdmx

Which type of Entity generator to use?

I am writing my first WPF and EF application. I am using SQL CE database and I have added few tables to the DB. The EF diagram is generated and now I want to generate the classes. I am new to EF and MVVM both.
When I right-click on a Table diagram, it gives option "Add Code Generation Item..". On selecting it, there are two options:
Add Entity Object Generator
Add Self-Tracking Entity Object Generator
I want to know what is the difference between the two. Which one should I use? I also want to know which one is latest and what is POCO?
A POCO is a Plain Old CLR Object... a simple class that has only properties.
http://en.wikipedia.org/wiki/Plain_Old_CLR_Object
There are 3 approaches that the Entity Framework delivers.
Model first (you create a model in visual studio and generate the database)
Database first (thats what you do, you generate a model from a existing database)
Code first (the newest one, you just write you POCOS and the entity framework generates the database)
I think it is enough to generate the diagram from database. The context and models should be available after this.
Neither of those is the POCO generator. The best way to get that is to install Entity Framework 4.1. You'll then see some new options in the list to add a code generation item.
I'm a pretty big fan of the DbContext/POCO generator added in 4.1 as the code it creates is VERY easy to work with compared to the older stuff, and it works well in a DB First setup like you're using (which is also what I use).
You can give this code generator a try:
http://salardbcodegenerator.codeplex.com/
It generates data annotations and implements INotifyPropertyChanged for CodeFirst approach.