I want to load child data from tables in entity framework using eager loading, but I am not getting the desired result. I want tb_Event data and then the customer data referenced by this table and then the data from EventLocation table and finally the data from tb_Facility and tb_Location based on tb_EventLocation.
This is what I did- till eventlocation I am gettting correct data but in tb_location and tb_Facility I am getting Cyclic dependency. Please help.
public IQueryable<tb_Event> Gettb_Event(){
db.Configuration.ProxyCreationEnabled = false;
return db.tb_Event
.Include(tb_Event=> tb_Event.tb_Customer)
.Include("tb_Event=>tb_Event.tb_EventLocation")
.Include("tb_EventLocation.tb_Location")
.Include("tb_EventLocation.tb_Facility");
}
Related
I have two repo method, one is retrieving single entity while the other one is retrieving a list of entities.
public async Task<Model> GetAsync()
{
return await _dbContext.Models.SingleOrDefaultAsync(s => s....);
}
public async Task<IList<Model>> GetListAsync()
{
var list = await _dbContext.Models.Select(s => .....).ToListAsync();
}
Based on the documentation of EF Core, related data will be loaded in Model object if Include() is called and vice versa. However, when I'm retrieving a list of entities and the related data get loaded regardless Include() is called. Is it by design?
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
I created my model using Db Context Generator, using EF 4.
My model is like this:
Program Table:
ID
Name
Group Table:
ID
Name
ProgramID (Associated to Program.ID)
I want to display these columns in my grid:
Program.Name - Group.Name
But grdGroups.DataSource = db.Groups.ToList()
doesn't return Program.Name
When I try to this I get ObjectDisposedException.
Partial Public Class Group
Public ReadOnly Property ProgramName() As String
Get
Return Program.Name
End Get
End Property
End Class
What's the best way to return the Program.Name to include it in the grid datasource?
When I try to this I get
ObjectDisposedException
The problem is lazy loading - EF did not materialize the related Program entity, hence when you try to access Program.Name it will try and re-query the DB, but the context has been disposed at this point, so you get an exception.
You can use an Include() query when you retrieve your Group entity, to specify that you also want to load the related Program entity, i.e. :
var groups = context.Groups.Include(x => x.Program);
I just spent the last 3-4 hours trying to retrieve a foreign key value using linq to entities and a stored procedure. Any advice is much appreciated.
public JsonResult GetEvents(double? start, double? end)
{
AnoEntities _dbAno = new AnoEntities();
var events = _dbAno.Events_GetByDateRange(fromDate, toDate);
var eventList = from e in events
select new
{
id = e.id,
title = e.title,
className = e.event_types.type.ToString()
};
return Json(eventList.ToArray());
}
type_id is the foreign key value that i'm trying to reach. I can't get it so appear in the entity data model and I can't seem to get to it. e.event_types and e.event_typesReference are both null so things like e.event_typesReference.EntityKey.EntityKeyValues.First().Value.ToString() aren't working.
Thanks!
I don't see any .Include methods or Load methods on even_types and I'm assuming your returning IEnumerable from your _dbAno.Events_GetByDateRange(fromDate, toDate). Like Craig pointed out in the comments if your return type of GetByDateRange is IQueryable you'd be projecting and EF should eager load for you.
Just a reminder that implicit lazy loading isn't supported out of the box in Entity Framework 1.0. You'll need to manually load the event_types with Load() or use the Include method on ObjectQuery.
I have an object that has been populated with the contents of four different related entities. However i have another entity in which i cannot include as part of the query due to it not being related in the navigation properites directly to the IQueryable table i am pulling. The entity i am trying to include is related to one of the four different entities that have been included successfully.
Is there a way to include(during db hit or afterwards) this entity as part of the overall object i am creating?
Here is an example of what my calls look like to build the CARTITEM object:
public List<CARTITEM> ListCartItem(Guid cartId)
{
//Create the Entity object
List<CARTITEM> itemInfo = null;
using (Entities webStoreContext = new Entities())
{
//Invoke the query
itemInfo = WebStoreDelegates.selectCartItems.Invoke(webStoreContext).ByCartID(cartId).ToList();
}
//Return the result set
return itemInfo;
}
here is the selectCartItems filter(Where i would normally do the includes):
public static Func<Entities, IQueryable<CARTITEM>> selectCartItems =
CompiledQuery.Compile<Entities, IQueryable<CARTITEM>>(
(cart) => from c in cart.CARTITEM.Include("ITEM").Include("SHIPPINGOPTION").Include("RELATEDITEM").Include("PROMOTION")
select c);
from this i have my CARTITEM object. Problem is i want to include the PROMOTIONTYPE table in this object, but since the CARTIEM entity doesn't have a navigation property directly to the PROMOTIONTYPE table i get an error.
Let me know if you need any more clarification.
Thanks,
Billy
You can use join and if it is the same database and server it should generate the join in SQL and do it all in one call...
LinqToEnties join example