Entity Framework - Modifying The edmx Automatically - entity-framework

I would like to change the names of some navigation properties in the model auto-generated from the database. How can I add a logic to modify the edmx automatically right after I use the wizard to generate the model from the database.

Related

EF6 Updating database from model overwrites my DefiningQuery

I'm using DefiningQuery to specify a Read only "view" entity that contains fields from two tables, and it works great for my need.
But each time I'm updating my model (using the Model designer) and runs the "Generate Database from Model..." operation, my DefiningQuery is lost because the .edmx file is getting overwritten.
The SQL being generated (in the .edmx.sql file) now assumes my Read Only entity must have a corresponding table created.
Is there a way to avoid the DefiningQuery being overwritten when the model is updated?
If possible I would like to avoid having to generate a Database View and map to that.
A little info:
I'm working on a project based on the Entity Framework 6.x.
I'm using the Model First approach.
The database is an SQL Server.

update model from database not updating code

I have a model in my website which was generated from a database. When I right click on the edmx diagram to update the model when I have made changes in my database (using mySQL), the model doesn't update the vb code file with any new tables and properties. I am using the standard .tt templates. Isn't the code supposed to regenerate every time the model is updated in this manner?

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".

Separating entity framework poco and objectcontext

so far i was creating a classLibrary project and inserting new Ado.net Entity data model and genareting from exixting database. Class and object class codes are creating automatically.
this is not important for me.
but i want to do this and separate the ObjectContext class (ex: SomeEntities) and table classess to two calss library.
when i change the database tables property, i will update edmx model and classes will update automatically.
is there any way to do this?
i am not using codefirst because have a database and datas in it,
i am not using modelfirst likewise,
i am using databasefirst but can not separate
Since you have "poco" in your title I guess that you are using the EF4 POCO Generator T4 Template.
Then yes, you can separate POCO classes and ObjectContext into two different class libraries. The T4 Template is prepared for that scenario since it consists of two different files:
POCOGenerator.Context.tt -> responsible to create your derived ObjectContext
POCOGenerator.tt -> responsible to create your POCO entities
If you add the POCO generator in the class library where you have your EDMX file, by default both tt-files will be added there.
But you can move then the second file (POCOGenerator.tt) into another class library. (The EDMX project where the context is located needs to reference this library to recognize the POCO classes.) After that open this file in a text editor. Some of the first lines in this file will look like:
...
string inputFile = #"MyModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
...
You now need to change the path to the edmx file (only in POCOGenerator.tt, leave POCOGenerator.Context.tt unchanged). Assuming you have edmx project and POCO project in the same solution of Visual Studio, the new path might be:
...
string inputFile = #"..\..\MyEDMXProject\MyModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
...
Now you can execute both files separately from two different projects. One will create the context file and the other will create your POCO files.

Making Entity Framework 4.0 create POCOs

When I create Entities within the Graphical view of the edmx file. All my entity classes are bundled together in the Designer file. Is there a way to make Entity Framework to create classes in separate files allowing me to have more control over my entity classes?
If you are using Visual Studio 2010, click on the design surface of the EDM Designer and select Add Code Generation Item, then select ADO.NET POCO Entity Generator. This will create a T4 Template file (*.tt) that will be used to generate your POCO classes. Every class will have it's own file.
You have to be aware of the fact that every time you make changes to your EDM and save, those classes will be re-generated and the files will be re-written, so it's better not to make any changes to them directly. Those classes are partial, so you can create new files and build up your classes without changing the initial files.