Maybe my google fu is sucking but I can't find any tutorial on how to use Entity framework without using the designer in Visual Studio. Is there a way to use Entity framework without the designer?
You can either use code first approach (it requires EFv4.1 or newer) where the mapping is defined in code (or by default mapping conventions) or you can create EDMX file from scratch - it is XML file.
They are two ways:
Classical (database first): You can learn about xml EF files on msdn .edmx File Overview (Entity Framework)
An .edmx file is an XML file that defines a conceptual model, a
storage model, and the mapping between these models. An .edmx file
also contains information that is used by the ADO.NET Entity Data
Model Designer (Entity Designer) to render a model graphically.
New (code first): They are a lot of resources. Here a MS code first video.
Related
I am learning EF and found two different methods for entity model file creation.
The first method uses ADO.NET Entity Data Model Template in Visual Studio which create a .edmx file, dbcontext file and .tt file.
The second method I found at https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db uses Scaffolding technique which uses a command like below
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
The second method just create the classes for the db tables and the dbcontext file but no .edmx file or .tt files metadata information being created. But the first method adds a connectionstring in app.config file with metadata information as well.
My doubt is what is the fundamental difference between these two? Are both methods acceptable in a development environment?
Your thoughts and inputs are highly appreciated.
Thanks
Peter
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".
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
After having used NHibernate for several years, I am now learning Entity Framework for use on my next project. At the moment, I am trying to decide between the code-first or model-first approaches. The model-first approach appeals to me, but the result would have to be ORM-agnostic. To this end, I am pondering the following. Would it be possible and practical to create T4 templates that generate classes and .hbm.xml files (for use with NHibernate) from the CSDL and C-S mapping content of a .edmx file?
No mapping is ORM agnostic because it is part of that ORM API or did you see Entity framework using NHibernate's hbm files? EDMX is XML representation of EDM (Entity Data Model) which is MS asset and it is mainly used for MS APIs. Moreover not all API for EDM processing provided by MS is accessible (internal implementation). EDMX is not ORM agnostic. The worse part is that it is even not database agnostic.
Anyway you can create any custom tool or transformation taking EDMX as input and providing other mapping as output. You just need to understand input and output format. I'm not sure if it will directly be possible with T4 but it is definitely possible. But it will not be practial. Practical is using single ORM to its full power and use tools available for that ORM.
It is possible and practical to create T4 templates that generate classes for Fluent NHibernate from an .edmx file. I am on a project at AMD where we are doing just that. (I am not the author myself.) See: http://tom-jaeschke.blogspot.com/2011/08/use-entity-framework-and-nhibernate.html
What is the purpose of .edmx files? Reading the CSDL, SSDL, and MSL specifications, it looks to me like .edmx files are used only at design time. Are we meant to distribute it with the other edmx? It seems like we need to distribute the .ssdl and/or .csdl files instead.
EDMX is Visual Studio's "container" for all things about your Entity Data Model.
It contains all the information that is in the CSDL, SSDL, MSL, plus information about the visual layout of the tables in your Visual Studio designer surface.
The EDMX file is converted into CSDL, SSDL, MSL (typically embedded as resources in your assembly) during the build process. You definitely don't have to distribute or copy the EDMX files anywhere for the app to run.
Update: if you are more interested in a code-based approach, you should check out the code-first CTP for Entity Framework which gets by without any .edmx, .csdl/ssdl/msl files at all.
An .edmx fileis an XML file that defines a conceptual model , a storage model, and the mapping between these models. An.edmx file also contains information that is used by the ADO.NET Entity Data Model Designer (Entity Designer) to render a model graphically.