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

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.

Related

Entity Frame work Coded first - Create Readonly EDMX Model

I am just starting out with EF Code-first:
If I create a read-only EDMX file from my code first can I save this readonly file as a stand-alone file and then include the resultant file in my project without affecting code first implementation.
Even though I am doing code first, I like a picture as it helps when I am explaining the db model to others. But I do not want to included this if it effects the code-first approach
will adding the EDMX file to the code-first project cause problems with the code-first approach?
No it doesn't have any effect on your code First approach. You can add the .edmx file directly to your project from Add-> New Item and generate the model from database without affecting your code-first approach. Just remember to use different namespaces so you don't get namespace conflict between your code-first and model classes.
To remove the Model, just remember to delete the related connection string in app.config/web.config to prevent your config files from becoming messy.

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

Entity Framework - Removing virtual keyword from navigation properties

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

Auto Generate entity in Entity Framework if a table is added to the schema

In Entity Framework, is there any way to make edmx file to automatically create entity when a table is added to the schema in the database and delete entity when a table is removed from the schema at run-time?
There is no EDMX file at runtime - there are only mapping files which are static XMLs. There is no mechanism controlling your database and modify these files if database changes (moreover as #marc pointed which files should be modified if you have more then one model?).
This is even doesn't make too much sense if you try to do it manually. Adding table to mapping is not enough - you also need a class and code which will use that class - both added at runtime.
There are more problems related to this. EF is tool configured at design time. The only exception is loading configuration classes for EF code first but again configuration classes are created at design time (I intentionally skip any ridiculous approaches with emitting MSIL at runtime).

Entity Framework - Foreign key constraints not added for inherited entity

It appears to me that a strange phenomenon is occurring with inherited entities (TPT) in EF4.
I have three entities.
1. Asset
2. Property
3. Activity
Property is a derived-type of Asset.
Property has many activities (many-to-many)
When modeling this in my EDMX, everything seems fine until I try to insert a new Property into the database. If the property does not contain any Activity, it works, but all hell breaks loose when I add some new activities to the new Property.
As it turns out after 2 days of crawling the web and fiddling around, I noticed that in the EF store (SSDL) some of the constraints between entities were not picked up during the update process.
Property_Activity table which links properties and activities show only
one constraint
FK_Property_Activity_Activity but
FK_Property_Activity_Property was
missing.
I knew this is an Entity Framework anomoly because when I switched the relationship in the database to:
Asset <--> Asset_Activity <--> Activity
After an update, all foreign key constraints are picked up and the save is successful, with or without activities in the new property.
Is this intended or a bug in EF?
How do I get around this problem?
Should I abandon inheritance altogether?
Not a but but a poor visual designer.
Its generally best to simply manage the Entity XML by hand.
No inheritance works well for many situations.
Basically I use the update from database in the visual designer but knowing that the designer has its quirks. I have simply used the update from database to stub out the basics of what I want. Then I go into the Entity XML my self and clean it up the way I want. Just of note Complex types are a pain with the designer. If you plan to use complex types get ready to learn your Entity XML well.