Entity Framework 5 Cyclic Lazy Loading Cause OutOfMemoryException - lazy-loading

I have problem with EF 5 and Lazy loading with cyclic references.
The below picture represents my model.
The main problem is between Model and ModelProperties classes, because Model contains IEnumerable navigation property and ModelProperty contains Model navigation property.
So this design cause the situation below
You can access fullsize image http://tinypic.com/r/2vskuxl/6
As you can imagine this cause very big problem, OutOfMemory exception.
Only solution that i could find is disabling lazy loading and using other methods. But lazy loading is very simplifying our work.
I hope there is a configuration or an attribute to help me loading only two levels of relations with lazy loading.
Is there any way to achieve this?
UPDATE:
Regarding the request from Julie Lerman, here is the visual model of EF.
I highlighted the main relation that cause problem.
Also you can access full-size at http://tinypic.com/r/30v15pg/6
UPDATE 2:
Here is the model definitions.
public class Model {
public int ModelID { get; set; }
public int BrandID {
get;
set;
}
public virtual Brand Brand { get; set; }
public string Logo { get; set; }
public string Name { get; set; }
public virtual ICollection<ModelProperty> ModelProperties {
get;
set;
}
}
public class ModelProperty {
public int ModelPropertyID {
get;
set;
}
public virtual int PropertyDefinitionID {
get;
set;
}
public virtual PropertyDefinition PropertyDefinition {
get;
set;
}
public virtual int ModelID {
get;
set;
}
public virtual Model Model {
get;
set;
}
public bool IsContainable {
get;
set;
}
public bool HasFilterDefinition {
get;
set;
}
public virtual ICollection<ModelPropertyValue> ModelPropertyValues {
get;
set;
}
public virtual ICollection<ModelPropertyMatchingFilter> ModelPropertyMatchingFilter {
get;
set;
}
}
Also there is an entity configuration for ModelProperty.
public class ModelPropertyEntityTypeConfiguration : EntityTypeConfiguration<ModelProperty> {
public ModelPropertyEntityTypeConfiguration() {
HasKey(p => p.ModelPropertyID);
HasRequired(p => p.PropertyDefinition).WithMany(s => s.ModelProperties).HasForeignKey(s => s.PropertyDefinitionID).WillCascadeOnDelete(false);
HasRequired(p => p.Model).WithMany(s => s.ModelProperties).HasForeignKey(s => s.ModelID).WillCascadeOnDelete(false);
HasMany(p => p.ModelPropertyValues).WithRequired(s => s.ModelProperty).HasForeignKey(s => s.ModelPropertyID).WillCascadeOnDelete(true);
HasMany(p => p.ModelPropertyMatchingFilter).WithRequired(s => s.ContainerModelProperty).HasForeignKey(s => s.ContainerModelPropertyID).WillCascadeOnDelete(false);
ToTable("dbo.ModelProperties");
}
}
UPDATE 3:
I am not sure but Automapper can cause this also. Because Entity Framework Profile tells thousands of Autommaper methods called while running.
UPDATE 4:
Here is the EFProf stacktrace:
You access bigger version http://tinypic.com/r/21cazv4/6
UPDATE 5
You can see sample project here: https://github.com/bahadirarslan/AutomapperCircularReference
In sample, you can see easily endless loops via Quick watch.

Thanks for the update. You're model looks fine. It definitely knows this is just a 1:* relationship. Ladislav (as usual) is correct. LL doesn't cause the problem...except in ONE place which is during serialization. Is there a chance that you have your code in a service? With regular lazy loading, only the property you explicitly mention will get lazy loaded. But during serialization, the serialization "mentions" every property so it just keeps loading and loading properties throughout the graph AND causes circular dependency issues. With services we have to turn lazy loading off (using context.configuration.lazyloadingenabled=false) before you return the data. So in the service method, you can eager load, or lazy load or explicitly load to get your graph but then disable lazy loading before you return the results.

you should disable lazy loading in order to bypass proxy objects or you should return what you need as a DTO. using DTO is preferred because of hiding details of your domain

Related

Lazy loading in EFCore loads all the data even if not accessed

I have been trying to use lazy loading in ASP.NET core 6, I have followed the documentation to do so. However, the behavior of the lazy loading is not the same as described in the docs
Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed. Here
builder.Services.AddDbContext<AppDbContext>(
options => options.UseLazyLoadingProxies()
.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
Currently I have the following entities
public class Book
{
public long Id { get; set; }
public string Name { get; set; }
public long AuthorId { get; set; }
public virtual Author Author { get; set; }
}
public class Author
{
public long Id { get; set; }
public string Name { get; set; }
}
And I have this endpoint
[HttpGet]
public IEnumerable<Book> GetBooks()
{
var list = _appDbContext.Books.ToList();
return list;
}
And the resulted SQL quires are
SELECT [b].[Id], [b].[AuthorId], [b].[Name]
FROM [Books] AS [b]
SELECT [a].[Id], [a].[Name]
FROM [Author] AS [a]
WHERE [a].[Id] = #__p_0
So in the endpoint I am not accessing the property Author inside Book entity, then why ef core is loading data that I dont need it kink of like eager loading but with two queries.
After investigation I realized that AutoMapper was accessing the Author so that is why its always retrieving the Author data.
So, lazyloading in EFCore is not convenient currently.

Can't load One To Many navigation property in EF Core

My question may be a littler more specific and I'm just learning EF Core. I have two classes. tblBuilding and tblBuildingHour. Simple classes. Don't mind the naming convention of the tables. It's a legacy database. It works fine if I remove the navigation properties. What am I missing? Is there something I need to do for the Castle proxy configuration? Is it the composite keys? Is it the lazy loading of the navigation property? I'm stumped.
public class tblBuilding {
public int ID {get;set;}
public string Name {get;set;}
public virtual ICollection<tblBuildingHour> BuildingHours {get;set;}
}
public class tblBuildingHour {
public int BuildingID {get;set;}
public DateTime BuildingHourDate { get;set; }
public DateTime StartTime {get;set;}
public DateTime EndTime {get;set;
public virtual Building Building {get;set;}
}
There's lazy loading of the entities in my db context.
services.AddDbContext<EMSDataContext>(options => options.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetSection(EmsDevDb).Value)
.UseLoggerFactory(_loggerFactory));
In the dbContext I added the one to many relationship.
modelBuilder.Entity<tblBuilding>()
.HasMany(b => b.BuildingHours)
.WithOne(r => r.Building)
.HasForeignKey(r => r.BuildingID);
The only odd thing in this tblBuildingHour table is that it has a composite keys so I don't know if that's what's messing it up.
modelBuilder.Entity<tblBuildingHour>()
.HasKey(c => new { c.BuildingID, c.BuildingHourDate });
I wondered if the lazyloading was affecting it so I tried this to no avail.
https://www.learnentityframeworkcore.com/lazy-loading
EF Core creates the relationships provided you create your entity classes correctly. After looking closer at my classes, I removed the fluent api relationship, changed my entity classes to have List instead of ICollection properties, removed .Include from my repo queries, and it worked like a charm. Eager loading wasn't the solution. Surprisingly enough, it wasn't even the composite keys. EF Core managed to create the relationships correctly after I removed mine.
Addendum: to be clear, removing .Include is not part of the fix, that was only for lazy loading.
REMOVED
modelBuilder.Entity<tblBuilding>()
.HasMany(b => b.BuildingHours)
.WithOne(r => r.Building)
.HasForeignKey(r => r.BuildingID);
UPDATED
[Table("tblBuilding")]
public class EMSBuilding
{
public int ID { get; set; }
public string Name{ get; set; }
public virtual List<EMSBuildingHour> Hours { get; set; }
}
[Table("tblBuildingHours")]
public class EMSBuildingHour
{
[Column("BuildingHoursDate")]
public DateTime BuildingHourDate { get; set; }
public DateTime OpenTime { get; set; }
public DateTime CloseTime { get; set; }
public int BuildingID { get; set; }
public virtual EMSBuilding Building { get; set; }
}
modelBuilder.Entity<EMSBuildingHour>()
.HasKey(c => new { c.BuildingID, c.BuildingHourDate });

Automapper not mapping between two objects (which are virtually the same for all intents and purposes)

Yes, this is ANOTHER "Automapper not mapping" question. Either something broke or I'm going the stupid way about it. I'm building a webapp with ASP.NET Core 2.1 using AutoMapper 3.2.0 (latest stable release at the time) though I have tested with 3.1.0 with no luck either.
Question
Simple object to be mapped to another. For the sake of testing and trials, these are now EXACTLY the same, yet still automapper gives:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
NotificationModel -> NotificationViewModel
ProjectName.Models.Dashboard.NotificationModel -> ProjectName.Models.Dashboard.NotificationViewModel
The strange thing is, I have previously mapped this model set 7 ways to sunday in the Startup.cs file with the only thing changing is my facial expression. Other maps work as indicated using similar, if not the same code for them.
The Models
NotificationModel.cs
public class NotificationModel
{
public int Id { get; set; }
public string Content { get; set; }
public DateTime CreateTS { get; set; }
public bool FlagRead { get; set; }
public bool FlagSticky { get; set; }
public bool FlagReceipt { get; set; }
public string ReceiptContact { get; set; }
public string UserId { get; set; }
public bool CANCELLED { get; set; }
}
NotificationViewModel.cs
public class NotificationViewModel
{
public int Id { get; set; }
//Reminder, this model has been amended to exactly represent that of the original model for testing purposes.
public string Content { get; set; }
public DateTime CreateTS { get; set; }
public bool FlagRead { get; set; }
public bool FlagSticky { get; set; }
public bool FlagReceipt { get; set; }
public string ReceiptContact { get; set; }
public string UserId { get; set; }
public bool CANCELLED { get; set; }
}
Startup & Automapper Config
Mapper.Initialize(cfg =>
{
// Some other mappings removed for clarity.
cfg.CreateMap<GroupViewModel, GroupModel>().ReverseMap();
//cfg.CreateMap<EntityViewModel, EntityModel>().ReverseMap().ForAllOtherMembers(opt => opt.Ignore());
cfg.CreateMap<NotificationModel, NotificationViewModel>().ForAllMembers(opt => opt.Ignore());
cfg.CreateMap(typeof(NotificationViewModel), typeof(NotificationModel));
//I even left out the .ReverseMap, for testing purposes.
});
Mapper.AssertConfigurationIsValid();
Usage
NotificationViewModel test = _mapper.Map<NotificationViewModel>(item); << Which is where I receive the exception.
Other Attempts
Ok, so I've been through some more articles explaining different things and subsequently tried the following respectively:
cfg.CreateMap(typeof(NotificationModel), typeof(NotificationViewModel));
cfg.CreateMap<NotificationModel, NotificationViewModel>().ReverseMap().ForAllMembers(opt => opt.Ignore());
cfg.CreateMap<NotificationModel, NotificationViewModel>().ForAllOtherMembers(opt => opt.Ignore());
Along with:
NotificationViewModel test = _mapper.Map<NotificationViewModel>(item);
_mapper.Map(item, typeof(NotificationViewModel), typeof(NotificationModel));
NotificationViewModel existingDestinationObject = new NotificationViewModel();
_mapper.Map<NotificationModel, NotificationViewModel>(item, existingDestinationObject);
I've tried amending the .Map()/.Map<> usage several ways, none of which seemed to yield anything but an exception about not having been configured.
So short of manually writing a conversion for this object (which is simple enough for its purpose), I am in dire need of a solution here. If not to use, then atleast to learn from and help others facing the same.
UPDATE
IT WORKS!
Scanning through the project, I noticed that somewhere in previous documentation - I read about creating a type of "config" class that just inherits from an abstract class called Profile. In this class you will also be able to define your maps, yet what is strange is that I am not able to drop this class and simply use the config maps setup in my Startup.cs file. Automapper will refuse to hold any maps that are not defined in this separate class. The below seems to get me what I need, however I still need an explanation as to why Automapper doesn't function as desired without it:
public class AMConfig : Profile
{
public AMConfig()
{
CreateMap<ManageUserModel, IndexViewModel>();
CreateMap<IndexViewModel, ManageUserModel>();
CreateMap<NotificationViewModel, NotificationModel>().ReverseMap();
CreateMap<List<NotificationViewModel>, List<NotificationModel>>().ReverseMap();
CreateMap<TaskViewModel, TaskModel>().ReverseMap();
}
}
Thanks!
Scanning through the project, I noticed that somewhere in previous documentation - I read about creating a type of "config" class that just inherits from an abstract class called Profile. In this class you will also be able to define your maps, yet what is strange is that I am not able to drop this class and simply use the config maps setup in my Startup.cs file. Automapper will refuse to hold any maps that are not defined in this separate class. The below seems to get me what I need, however I still need an explanation as to why Automapper doesn't function as desired without it:
public class AMConfig : Profile
{
public AMConfig()
{
CreateMap<ManageUserModel, IndexViewModel>();
CreateMap<IndexViewModel, ManageUserModel>();
CreateMap<NotificationViewModel, NotificationModel>().ReverseMap();
CreateMap<List<NotificationViewModel>, List<NotificationModel>>().ReverseMap();
CreateMap<TaskViewModel, TaskModel>().ReverseMap();
}
}

Automapper and NHibernate lazy loading

I am struggling with this issue:
I have a list of NHibernate objects called "Project". These objects contain a lazy - loaded list of "Branches". I am trying to pass a list of Projects to a WCF service so I am using AutoMapper to transform them to flat objects.
The problem is that even though the destination objects called "ProjectContract" does not contain a list of Branches, Automapper still invokes this collection and a lot of queries are made to the database because NHibernate fires the lazy - loading and loads the Branches collection for each project.
Here are the classes and the mapping:
public class Project
{
public virtual int ID
{
get;
set;
}
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<Branch> Branches { get; set; }
}
[DataContract]
public class ProjectContract
{
[DataMember]
public virtual int ID
{
get;
set;
}
[DataMember]
public virtual string Name { get; set; }
[DataMember]
public virtual string Description { get; set; }
}
public class ProjectMappings : Profile
{
protected override void Configure()
{
Mapper.CreateMap<Project, ProjectContract>();
}
}
My question is: Is there a way to tell AutoMapper to not touch the "Branches" collection because I don't care about it and that is a proxy that will trigger many database calls?
I temporarily fixed this with MaxDepth(0), but there are other entities where I have collections that I want to transfer, and collections that I don't want to be touched, like this one. In that case, MaxDepth(0) will not work.
Thank you,
Cosmin
Yes, The AutoMapper Ignore function.
Mapper.CreateMap<Source, Destination>()
.ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());

Why my EF collection are not lazy?

I'm using EF 4.1 Code first. I have a model of user and a model of setting.
Each time the repository returns a user the Setting is also loaded. I've marked the Setting as virtual all my access modifiers are public LazyLoadingEnabled and ProxyCreationEnabled are enabled by default.
What am I missing?
public class User : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual ICollection<Setting> Settings { get; set; }
}
public class Setting
{
public int UserID { get; set; }
public int SettingID { get; set; }
public string Value { get; set; }
}
The User might have several setting, so there is a one to many relationship with a foreign key in setting.
The user configuration is
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
HasKey(u => u.ID);
HasMany(u => u.Settings).WithOptional().HasForeignKey(u => u.UserID);
}
}
and the Setting configuration is:
public class SettingsConfiguration : EntityTypeConfiguration<Setting>
{
public SettingsConfiguration()
{
ToTable("UserSettings");
HasKey(s => new { s.UserID, s.SettingID });
}
}
Lazy loading means the opposite of what you think it means.
With lazy loading (virtual property and defaults)
Settings is not retrieved immediately when querying User
Settings is retrieved when it's accessed for the first time. The DbContext must be open at that time for this to happen; otherwise you'll get an exception
Without lazy loading (non-virtual property and/or explicitly disabled)
Settings is not retrieved immediately when querying User
Settings will never be retrieved automatically; it will return null (which, in my opinion, is a terrible design decision: null is a wrong value and you shouldn't be able to get it)
In both cases, you can load Settings eagerly by using .Include(x => x.Settings), or when needed, by calling context.Entry(user).Collection(x => x.Settings).Load()