Separating entity framework poco and objectcontext - entity-framework

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.

Related

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.

Get DbContext for Entities

Okay, I feel a bit foolish for having to ask this but I guess my understanding of the inner workings of Entity Framework is lacking.
I'd like to experiment with work with DbContext. I have an existing ASP.NET MVC application using EF 4.2. I can get my entities using:
var context = new MyEntities();
And this works just fine.
But how the heck to I get the same data represented by a DbContext?
So I guess you are using default code generator provided by EDMX designer - it will use ObjectContext and heavy weight EntityObject based entities.
If you want to use DbContext you must:
Turn off that default code generation - in property window remove Custom Tool for EDMX file
Download and install DbContext T4 generator (you can get it directly from extension manager in Visual Studio)
In EF designer select Add Code Generation Item from context menu in the designer surface (not on entity)
Now EF will add two .tt files to your project - one will be responsible for creating a new class for every entity or complex type defined in your EDMX file and the second will be responsible for creating class derived from DbContext and exposing sets for all your entity types

Query in Ado.net Entity Object Generator template

I used Ado.net Entity Object Generator template to generate the business objects from the Edmx file. By default it is adding two files namely
Model.tt
Model.Context.tt
The Model.tt template is generating the business objects, can any one explain the use of Model.Context.tt?
Model.Context.tt generates your derived context exposing all ObjectSets, function imports etc. You can further extend this context in your own partial class and add custom methods you need. ObjectContext is entry point to EF functionality.

Creating POCO Classes on Phasis

I create persistence ignorant classes for an existing DB which is very big and i don't want to create all classes in the beginning i want to create only classes i need and create each class when i need it.
Is there any feature in Entity Framework can do this?
You can build incrementally your EDMX file adding only entities you need to use at the moment. Than you need to define only POCO classes defined in your EDMX file or you can use POCO template which will generate POCO classes for you.

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.