Entity Framework Include method - entity-framework

when i try to use include i can't access to table context.Users, only to it's fields
Where is my mistake?
Thanks.

Using the Load method means you are some how trying to use Eager loading of certain requirements, what you could try and do is turn off Lazy Loading, this way all queries will load related entities without having to use the Include Method. They proxy entities will handle the references between the entity relationships for you.
Once you return Doctors in your View, and you loop through them
foreach(var doctor in Model) { #doctor.PhoneNumber.EmergencyNumber }
PhoneNumber will be proxied by EF and you will not need to use any Eager Loading through the Include function.
public Context() : base("DefaultConnection")
{
Configuration.ProxyCreationEnabled = true; // Our Navigational Properties
Configuration.LazyLoadingEnabled = false; // No need to Include with Eager Loading
Configuration.AutoDetectChangesEnabled = true; // Change Tracker for changes made on either side of the Association through Proxy Class
Configuration.ValidateOnSaveEnabled = true; // Validate On Saving for Data Integrity
}

you should use context.Doctor.Include(d=>d.PhoneNumbers)
this will extract all doctors with filled phone numbers per each

Related

There is already a generated proxy type for the object layer type 'MyProject.Model.Applications'

I am using a code first approach. I am setting up a process to copy table rows from sourcedb to targetdb on daily basis. Also I need to maintain the primary key values in both. (this is required) i.e. Both dbs should have same primary key for given row.
I have created 2 different contexts by referring same classes. Both contexts are intact. I am getting all the rows from sourcedb into a list of object and passing it to another context to insert that range into targetdb. But while doing so I am getting the error as 'There is already a generated proxy type for the object layer type 'MyProject.Model.Applications'. This occurs when the same object layer type is mapped by two or more different models in an AppDomain.
I have checked some other links. But nothing has worked so far. I have also checked is it possible to share POCO object between two DbContext?.
Following is some pseudo code,
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(6000)))
{
using (var dbContext = new SourceDbContext())
{
DateTime dateToBeCompared = DateTime.UtcNow.Date.AddMonths(-11);
dbContext.Configuration.LazyLoadingEnabled = false;
dbContext.Configuration.AutoDetectChangesEnabled = false;
//get data from application related tables.
var applications = dbContext.Applications.AsNoTracking().Where(a => a.UpdatedOn <= dateToBeCompared)
.ToList();
using (var connection1 = new System.Data.SqlClient.SqlConnection("TargetDbConnectionString"))
{
connection1.Open();
using (var targetDbContext = new TargetDbContext(connection1, true))
using (TransactionScope tsSuppressed = new TransactionScope(TransactionScopeOption.Suppress))
{
targetDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Applications] ON");
}
try
{
targetDbContext.Applications.AddRange(applications);
targetDbContext.SaveChanges();
}
catch (Exception ex)
{
throw;
}
using (TransactionScope tsSuppressed = new TransactionScope(TransactionScopeOption.Suppress))
{
targetDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Applications] OFF");
}
}
connection1.Close();
}
scope.Complete();
}
Also there are some foreign key constraints. But whatever is there, common to both contexts.
Neither dbContext.Configuration.LazyLoadingEnabled = false; nor AsNoTracking() prevents EF from creating proxy objects in the source context, which in turn prevents using them in another context.
What you really need is to turn ProxyCreationEnabled off:
Gets or sets a value indicating whether or not the framework will create instances of dynamically generated proxy classes whenever it creates an instance of an entity type. Note that even if proxy creation is enabled with this flag, proxy instances will only be created for entity types that meet the requirements for being proxied. Proxy creation is enabled by default.
It also prevents lazy loading because it depends on proxy class intercepting the virtual properties. So simply replace
dbContext.Configuration.LazyLoadingEnabled = false;
with
dbContext.Configuration.ProxyCreationEnabled = false;
and the issue will be resolved. Just note that this doesn't mean the code will work correctly. The entities must have explicit FKs defined (not simply navigation properties) and the related entities must be processed first to prevent FK constraint violation.

Combining lazy and eager loading in EF

I am wondering if it is possible combining lazy and eager loading.
For example, I have one Web Api controller with GET method that does not need to load related entity data, but I have also another Web Api controller and its method GET needs to retrieve data from related entity.
Is it good practice to combine these two approaches and are there any specific configurations I need to set up?
Yes you can do that.And it's a Good practise too according to the real situation like yours.
When you don't need the Lazyloding ,on that particular method you can disable it as shown below.
public List<PropertyListDto> SearchProperties(AdditionalSearchInput input)
{
_context.Configuration.LazyLoadingEnabled = false;//to remove lazy loading
///your code
}
Note : In Entity Framework 4 and beyond Lazy Loading is enabled by default. We can disable it globally, on DbContext level, or selectively, on query level as shown above.
Here is how to do it on DbContext level.
public partial class MyDBEntities : DbContext
{
public MyDBEntities(): base("name=MyDBEntities")
{
this.Configuration.LazyLoadingEnabled = false;
}
}
Update : The 50 controllers where you don't need lazyloding you can disable it on constractor level as shown below.Then you don't need to give it on query level on each method.I think it's very quick way to implement it :)
public class YourAppService : IYourAppService
{
private readonly YourDbContext _context;
public YourAppService(YourDbContext context)
{
_context = context;
_context.Configuration.LazyLoadingEnabled = false;//to remove lazy loading
}
}

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

I'm having trouble with one of my queries because of EF's change tracking and lazy loading features. The thing is that after I'm getting the result of the query, I'm using AutoMapper to map the domain objects into my business model but it keeps throwing an exception because the context has been disposed.
The ObjectContext instance has been disposed and can no longer be used
for operations that require a connection.
When I look at the resultant collection in the debugger, I see that it is a list of DynamicProxy and not the actual entity. I tried to stop Change Tracking but that did not help. Here's my code:
public List<ContentTypeColumn> GetContentTypeColumns(Int64 contentTypeId)
{
List<ContentTypeColumn> result = new List<ContentTypeColumn>();
using (SCGREDbContext context = new SCGREDbContext())
{
ContentType contentType = context.ContentTypes.Include("Parent").AsNoTracking().FirstOrDefault(x => x.Id.Equals(contentTypeId));
result.AddRange(contentType.ContentTypeColumns.ToList());
while (contentType.Parent != null)
{
result.AddRange(contentType.Parent.ContentTypeColumns.ToList());
contentType = contentType.Parent;
}
}
return result.ToList();
}
Note: If you need to look into my domain model involved in this operation you can refer to this question.
If you need to stop lazy loading and dynamic change tracking you can simply turn it off:
using (SCGREDbContext context = new SCGREDbContext())
{
context.Configuration.ProxyCreationEnabled = false;
...
}

What is the best way to add attributes to auto-generated entities (using VS2010 and EF4)

ASP.NET MVC2 has strong support for using attributes on entities (validation, and extending Html helper class and more).
If I generated my Model from the Database using VS2010 EF4 Entity Data Model (edmx and it's cs class), And I want to add attributes
on some of the entities. what would be the best practice ? how should I cope with updating the model (adding more fields / tables to the database and merging them into the edmx) - will it keep my attributes or generate a new cs file erasing everything ?
(Manual changes to this file may cause
unexpected behavior in your
application.)
(Manual changes to this
file will be overwritten if the code
is regenerated.)
Generally you'd create what is called partial classes to extend your auto-generated objects.
Adding Attributes to Generated Classes
With the "buddy class" concept, linked above, and data annotations I use this extention method. I forget where I got it, so kudos to the original author.
We use it like
List<ValidationResult> errorList = new List<ValidationResult>();
bool bValid = client.IsValid<Client, ClientMetadata>(ref errorList, false);
public static bool IsValid<T, U>(this T obj, ref List<ValidationResult> errors, bool validateAllProperties = true) where T : IValidatableObject
{
//If metadata class type has been passed in that's different from the class to be validated, register the association
if (typeof(T) != typeof(U))
{
TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(T), typeof(U)), typeof(T));
}
var validationContext = new ValidationContext(obj, null, null);
var validationResults = new List<ValidationResult>();
Validator.TryValidateObject(obj, validationContext, validationResults, validateAllProperties);
errors = validationResults;
if (validationResults.Count > 0)
return false;
else
return true;
}
We use partial classes, but if you need them persisted and handled by EF, the "Update Model from Database" option is your best friend.

MVC 2 and EF4 Self-tracking entities models have bad state on post back

I've got standard Create() Edit() and Delete() methods on my controllers, and I am using the EF4 Self-tracking entities.
When the edit is posted back, the model.ChangeTracker.ChangeTracking = false, and model.ChangeTracker.State = ObjectState.Added, even though I made sure those are set when retrieving the record initially.
Are the self-tracking entities not persisting the ChangeTracker class when the form is submitted? If so, how do I fix that?
public virtual ActionResult Edit(int personId)
{
IContext context = ContextFactory.GetContext();
EntityRepo Repo = new EntityRepo(context);
Person d = Repo.Person.GetById(PersonId);
d.ChangeTracker.ChangeTrackingEnabled = true;
return View(d);
}
[HttpPost]
public virtual ActionResult Edit(int personId, Person item)
{
try
{
if (ModelState.IsValid)
{
IContext context = ContextFactory.GetContext();
EntityRepo Repo = new EntityRepo(context);
// the item is returning these properties that are wrong
//item.ChangeTracker.ChangeTrackingEnabled = false;
//item.ChangeTracker.State = ObjectState.Added;
Repo.Person.Update(item);
Repo.Person.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
}
return View();
}
Let's start at the beginning.
What are Self-Tracking Entities, exactly?
A Self-Tracking Entity is an entity which can do change tracking even when it is not connected to a ObjectContext. They are useful in times when you must change the entity, but cannot have it connected to an ObjectContext.
So when would I want one, really?
Mostly, when you must have distributed objects. For example, one use case is when you are making a web service which talks to a Silverlight client. However, other tools, like RIA Services may be a better fit here. Another possible use case is for a long-running task. Since an ObjectContext is intended to be a unit of work and should typically not be long-lived, having a disconnected entity might make sense here.
Do they make any sense for MVC?
Not really, no.
Let's look at this a little deeper, and examine what happens when you update an entity in MVC. The general process is like this:
The browser issues a GET request for an update page.
The MVC app fetches an entity, and uses it to build an update HTML page. The page is served to the browser, and most C# objects, including your entity, are disposed. At this point, you can restart the Web server, and the browser will never know the difference.
The browser issues a POST request to update the entity.
The MVC framework uses the data in the POST in order to materialize an instance of an edit model which is passed to the update action. This might happen to be the same type as the entity, but it is a new instance.
The MVC app can update the entity and pass those changes back to the database.
Now, you could make self-tracking entities work by also including the full state of the STE in the HTML form and POSTing that back to the MVC app along with the scalar values on the entity. Then the Self-Tracking Entity might at least work.
But what benefit does this give you? The browser obviously cannot deal with your entity as a C# object. So it cannot make any changes to the entity worth tracking in terms that a Self-Tracking Entity would understand.
U should keep original STE in some hidden field. It's like your custom ViewState. In submit method u must merge original STE and new values.
Use ActionFilterAttribute for it.
Like
public class SerializeOriginalModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var viewResult = filterContext.Result as ViewResult;
if (viewResult == null)
return;
var viewModel = viewResult.ViewData.Model as ViewModel;
if (viewModel == null || viewModel.SteObject == null)
return;
byte[] bytes;
using (var stream = new MemoryStream())
{
var serializer = new DataContractSerializer(viewModel.SteObject.GetType());
serializer.WriteObject(stream, viewModel.SteObject);
bytes = stream.ToArray();
}
var compressed = GZipHelper.Compress(bytes);
viewModel.SerializedSteObject = Convert.ToBase64String(compressed);
}
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionParameters == null || filterContext.ActionParameters.Count == 0)
return;
var viewModel = filterContext.ActionParameters.First().Value as ViewModel;
var serialized = filterContext.HttpContext.Request.Form["SerializedSteObject"];
if (viewModel == null || String.IsNullOrEmpty(serialized))
return;
var type = filterContext.ActionParameters.First().Value.GetType().BaseType.GetGenericArguments()[0];
var bytes = GZipHelper.Decompress(Convert.FromBase64String(serialized));
using (var stream = new MemoryStream(bytes))
{
var serializer = new DataContractSerializer(type);
viewModel.SteObject = serializer.ReadObject(stream);
}
}
}
STE has one very big drawback. You have to store them in session or view state (WebForms). So it is nothing more than "new version of dataset". If you don't store STE you will have one instance for getting data and different for posting = no change tracking.
I think you are missing the idea of Repository. You should not have an Update method in the Repository. After submitting, you should get the item again, apply the modifications and then Save.
I prefer having a service layer between client and Repository. We can always change the strategy with which we merge.
And yes, if you need to persist your STE's between requests, use session or viewstate.
It should be
Repo.Person.ApplyChanges(item);
Repo.Person.SaveChanges();
instead of
Repo.Person.Update(item);
Repo.Person.SaveChanges();
Self Tracking works with ApplyChanges extention method.