Entity Framework - Removing virtual keyword from navigation properties - entity-framework

I am using entity framework with database first approach, along with EF 4.x DBContext Generator. All my entity classes have navigation properties which are marked as "virtual". I want to remove virtual keyword from all my entity classes. There are around 350 entity classes

You must modify T4 template (.tt) file to remove virtual keyword.

Removing virtual will mean that you are unable to use lazy loading for entity framework. In my opinion lazy loading has a very limited scope and is mostly misused (which often causes performance issues). However do note that when you turn it off you may need to adjust your code to manually load additional navigation properties when you retrieve data. You can do this by using .Include in your EF query
To remove the virtual flag you probably need to disable lazy loading in your EF generator.

You may want to try this VS extension, It adds (among other things) fine control over the virtual modifier:
EF Designer Extender

Related

Code-first models' relation definitions without virtual fields?

Is there any way to create one-to-many and many-to-many relationships without having to use virtual fields? I am asking because I would be happy to reuse models created for the code first migration, but I do not know what to do with those virtual fields. I just need a plain model without features like that.
P.S. I am using EF only for migration, so there is no issue with breaking the framework.
The virtual keyword is not necessary. They are only necessary if you want to achieve Lazy-Loading for navigation properties. If you are using EF only for migration, you could remove the virtual keyword without problems.

Can I have hand-written simple POCO's without any FixupCollection<T> like the template generator generates?

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

Change MyEntityFrameworkModel.edmx.cs File

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

Does Entity Framework 4 not support property automatic lazy loading for model-first entities?

All references that I find for lazy loading say it's possible but they all mention POCOs and that's it. I am using EF4 with the model-first methodology. In my model diagram I have a Project table and a UserObject table, with a 1 to many relationship between them. However, in code, when I have a valid UserObject and I attempt to get the project performing: Project prj = userobj.Project. Unfortunately, this doesn't work as it claims that UserObject.Project is null.
It seems like I have to explicitly load the Project object via calling UserObject.ProjectReference.Load() prior to calling .Project. Is there any way for this to occur automatically when I access the .Project property?
This should work just fine. Right click on the EDMX, click Properties, check that Lazy loading enabled is set for the EDMX.

Entity framework POCO

What does one loose by creating POCO using T4 templates in entity framework 4.0? Why is the default behavior when using entity framework 4.0 not to create POCO?
You lose a number of things. A "pure" POCO is of limited use in an ORM, because it will not do change tracking. In other words, when you mutate the object and then save changes to the context, you would like the changed properties saved to the database. With a "pure" POCO you can do this with snapshot based change tracking, which is fairly inefficient. You can also do it with runtime proxies, which force you to make your track properties public virtual, so you arguably don't have a "POCO" anymore. Also, using proxies means that you don't know the true runtime type of the instance.
You also lose some of the convenience properties like EntityState.
"Pure" POCOs cannot do lazy loading. Again, you can work around this with proxy types, but, again, if you're using proxies, you don't really have a "pure" POCO.
On top of all of this, there is less need to use POCO entities in the Entity Framework than in some other ORMs. This is because you can always project your entity types onto POCO instances using LINQ, without having to materialize the entity instances first. So "pure" POCOs are always available in an Entity Framework application, even if you don't happen to map your entities that way.