I was wondering if can change the edmx.cs file (change inheritance and base constructors of object context derived class).
when I try this , all changes will defect as i build the project.
Note I mean changing the object context derived class not entity classes.
thank you.
If you can afford it (meaning if your project is not too complex already), I could suggest that you switch to code-first style (EF 4.1). That allows you to build all the inheritance you want in your objects. And since you create your own context by inheriting DbContext, you also have total flexibility there.
You can use your EDMX (with the T4 template packed in EF 4.1) or your existing database to create the classes (so at least what you have done until today still stands).
http://thedatafarm.com/blog/data-access/quick-look-at-reverse-engineer-db-into-code-first-classes/
http://devlinliles.com/post/Reverse-Engineer-Code-Firste28093Jump-start-for-existing-Databases.aspx
The partial class solution would maybe do it too (depending on what you wish to achieve).
To change in entity frame work class its better to create Shared classes with same name and add your own methods and properties
Related
I am using Entity Framework and want to do some changes in context class
For ex. I am creating one more constructor for connection string but issue is every time when I open .edmx file and save it, then my changes are no longer exist.
Is there any way for me to tell .edmx not to undo my changes in context class or T4 classes?
You cannot change the generated code. You can add things to it since the classes that are generated are partial classes.
Add your additions to a separate file containing the custom part of the partial class.
See here: https://msdn.microsoft.com/en-us/library/wa80x488.aspx
An alternative is to modify the T4 template yourself and consistently apply your specific needs to the generated classes.
It is common practice to do this when for example you want INotifyPropertyChanged as interface on every class.
This approach only works if your changes are generic in nature. If you want to do a specific change in only one class then this solution does not work for you.
Another alternative is to take full control of the classes by using code first.
You can mark the methods and properties with special attributes to map them to the db. And there is a reverse engineering option to create these classes from an existing db. You would reverse engineer once, and then take control of the classes and tweak them to your needs
I generally take the last approach myself right now, because it is not using partial classes all code that belongs to a class is all in the same file.
The answer of Maarten is also fine, but there are 2 more options.
I want to hand-write my POCO's as simple classes with virtual properties so that the entity framework can generate proxies. I will enable proxy creation (which is on by default) so I can have change tracking and lazy loading.
My question is: can I have them?
More specifically, I noticed that the POCO template generator generates classes with a FixupCollection<T>. Is that necessary to have? If I make the navigational properties in my simple hand-written POCO a virtual IEnumerable<T>, will that suffice without any FixupCollection<T> stuff?
As an alternative, I could use the POCO template generator but I do not wish to, because my model is quite large and complicated. I will need to keep making changes to the model and will need to keep editing the model class definitions. If I use the POCO template generator, every time I need to regenerate the model for some reason, it will overwrite all my custom changes.
So, is it possible to just have POCO's in the real spirit of their name?
Yes, of corse it is possible.
http://msdn.microsoft.com/en-us/library/vstudio/dd456853%28v=vs.100%29.aspx
FixupCollection class can be used by the POCO classes to keep the opposite ends of a relationships in sync.
http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx
I've tried to use Entity Framework 4 and POCO for my MVC 3 project. May be, I don't understand the main idea of this ORM, but the problem is following:
I added ADO .NET Entity Data Model and make model according to database.
I clicked Add Code Generation Item and added ADO .NET POCO Entity Generator.
It makes classes for every database table.
I want to add some methods to work with data (Add, Update, Delete, GetAll etc) to appropriate models.
For LINQTOSQL I added partial classes and placed them to Models. But now I can't do it because:
a) Models folder has classes with the same names, which was created by POCO.
b) If I place my partial class in the another folder, it will be another namespace - so, such classes won't be partial one.
c) If I place my code in POCO classes, it can be destroyed during update POCO.
How can I use it? Where sould I place my methods for data working?
Is the best way to make for POCO and EF the other project - http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx?
First of all you don't have to write your CRUD inside POCO,
There are many places where you can do it like in edmx.cs file or write one more layer which is called as CRUD Services which handles the Database operations using context object.
Now coming to your questions,
Create separate Models folder and place the Model classes in there.
Your Model class may like this,
EmployeeDepartmentModel
{
prop EmpList List(Emp);
prop DeptList List(Dept);
//Emp and Dept are my POCOs
}
So now I have to fill both of these list(Your CRUD question),
For that, I will Create one method in Controller class(its better to write such logic in some another library, but for time being I suggest you to create in Controller),
FillTheModel()
{
EmployeeDepartmentModel.EmpList = EDMX.GetAllEmployees;
EmployeeDepartmentModel.DeptList = EDMX.GetAllDepartments;
}
Now you can bind this model with your view.
You can place the partial classes in another folder and modify the namespace.
I agree with allisewell, but if you really want to add parts to partial classes, give files another name,
e.g. MyPoco.Part2.cs or modify t4 template to name generated files
e.g. Poco.Generated.cs
I use standard ObjectContext and EntityObjects in my application. Let's say two of my tables are Projects & Services. Projects have Subproject (from Projects table with ParentID == ProjectID) and also Services. So I would have a hierarchy like Projects->Subprojects->Services. But I need to inherit Projects and Services from an abstract base class so I can use any of these entities as a new Task/Job entity in my application. Then, for example I can create a TreeList listing all Tasks (either a Project or Service). Is there anyway in EDMX designer I can create a new type (entity) which is the base calss for two or more concrete types?
It is possible with TPC inheritance but it will include a lot of complication to your design. For example:
you will have to move shared properties to the base class
you will probably have to maintain some mappings manually in EDMX (at least I had when I did the sample on screenshot)
you will have only single ObjectSet<Tasks> and you will have to use OfType to query only Projects or Services
you will have to use unique Id per Task = across both Project and Service tables (can be achieved by correctly configured identities in database)
It will look like:
Another option is using interface on your entity objects instead of parent class. You can define interface in your partial part of entity object and handle retrieving both Projects and Services by yourselves where your UI will expect only list of types implementing your interface.
http://blogs.microsoft.co.il/blogs/gilf/archive/2010/01/25/table-per-concrete-type-inheritance-in-entity-framework.aspx
Since it sounds like your data is coming from 2 separate tables, Projects and Services, no, I don't think you can achieve this in the designer (at least, not without hand-editing the generated edmx). If there were a common table to represent the base class, that could be done in the designer, but that doesn't sound like it fits your situation.
What you may be able to do is use an interface instead of an abstract base class, and use partial classes in your entity model to implement the interface for each of your entities. You can't directly inherit from your abstract base class in your entity model, because all of your entities already derive from EntityObject. If you have a lot of shared implementation that resides in your base class, it might be worthwhile to switch to POCO, where you can define your own inheritance hierarchy.
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.