question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Abp.ZeroCore.EntityFramework errors with indexes and lazy loading

See original GitHub issue

There are some bugs to be fixed with the Abp.ZeroCore.EntityFramework package in order to get it working with an ASP.NET Boilerplate ASP.NET Core project targeting the full .NET Framework and Entity Framework 6.

I have set a repository with the vanilla aspnetboilerplate template using asp.net core and EF6 here: aspnetboilerplate-core-entityframework6

Problems with indexes

EntityFramework 6 does not like adding indexes with key columns of type string with no length limit. In order to make migrations work I had to add the following code to my DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
	base.OnModelCreating(modelBuilder);

	// HACK: Added maximum length to avoid Index validation errors. EF6 does not allow nvarchar(MAX) for indexes
	modelBuilder.Entity<RoleClaim>().Property(rc => rc.ClaimType).HasMaxLength(256);
	modelBuilder.Entity<UserClaim>().Property(uc => uc.ClaimType).HasMaxLength(256);
	modelBuilder.Entity<UserAccount>().Property(ua => ua.UserName).HasMaxLength(256);
	modelBuilder.Entity<UserAccount>().Property(ua => ua.EmailAddress).HasMaxLength(256);
}

So I recommend you to add this length restrictions to those properties using constants like it’s already done for some other entities.

Problems with lazy loading

The provided respository project does build and EF migrations run correctly. But when I try to login through the web project or even running the unit tests, I am getting the following exception:

Message: System.InvalidOperationException : The property 'Tokens' on type 'User_259C3D76EAF67125930DAB5AF9DD530621C9839E2069AFE32437A1ECA12793DE' cannot be set because the collection is already set to an EntityCollection.
	in System.Data.Entity.DynamicProxies.User_259C3D76EAF67125930DAB5AF9DD530621C9839E2069AFE32437A1ECA12793DE.set_Tokens(ICollection`1 )
   in Abp.Authorization.Users.AbpUser`1..ctor()
   in AbpCompanyName.AbpProjectName.Authorization.Users.User..ctor()
   in lambda_method(Closure , Shaper )
   in System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   in lambda_method(Closure , Shaper )
   in System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   in System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   in System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   in System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
   in System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
   in System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   in System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
   in System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   in AbpCompanyName.AbpProjectName.Tests.AbpProjectNameTestBase.<>c__DisplayClass18_0.<LoginAsTenant>b__1(AbpProjectNameDbContext context) in aspnetboilerplate-core-entityframework6\\aspnet-core\\test\\AbpCompanyName.AbpProjectName.Tests\\AbpProjectNameTestBase.cs:line 244
   in AbpCompanyName.AbpProjectName.Tests.AbpProjectNameTestBase.UsingDbContext[T](Nullable`1 tenantId, Func`2 func) in aspnetboilerplate-core-entityframework6\\aspnet-core\\test\\AbpCompanyName.AbpProjectName.Tests\\AbpProjectNameTestBase.cs:line 177
   in AbpCompanyName.AbpProjectName.Tests.AbpProjectNameTestBase.UsingDbContext[T](Func`2 func) in aspnetboilerplate-core-entityframework6\\aspnet-core\\test\\AbpCompanyName.AbpProjectName.Tests\\AbpProjectNameTestBase.cs:line 137
   in AbpCompanyName.AbpProjectName.Tests.AbpProjectNameTestBase.LoginAsTenant(String tenancyName, String userName) in aspnetboilerplate-core-entityframework6\\aspnet-core\\test\\AbpCompanyName.AbpProjectName.Tests\\AbpProjectNameTestBase.cs:line 241
   in AbpCompanyName.AbpProjectName.Tests.AbpProjectNameTestBase.LoginAsDefaultTenantAdmin() in aspnetboilerplate-core-entityframework6\\aspnet-core\\test\\AbpCompanyName.AbpProjectName.Tests\\AbpProjectNameTestBase.cs:line 212
   in AbpCompanyName.AbpProjectName.Tests.AbpProjectNameTestBase..ctor() in aspnetboilerplate-core-entityframework6\\aspnet-core\\test\\AbpCompanyName.AbpProjectName.Tests\\AbpProjectNameTestBase.cs:line 55
   in AbpCompanyName.AbpProjectName.Tests.Users.UserAppService_Tests..ctor() in aspnetboilerplate-core-entityframework6\\aspnet-core\\test\\AbpCompanyName.AbpProjectName.Tests\\Users\\UserAppService_Tests.cs:line 15

So the problem is that in Abp.Authorization.Users.AbpUser the constructor is initializing the collection Tokens as a new Collection of UserTokens, see code below:

protected AbpUser()
{
    Tokens = new Collection<UserToken>();
}

When EF6 lazy loading tries to initialize that collection with its virtual collection wrapper, it throws an exception beacuse that property is already initialized, so you need to find a solution to have lazy loading to work properly.

Summary

Those errors are preventing a vanilla ASP.NET Boilerplate application to work properly when using the ZeroCore module and EntityFramework 6 at the same time. I will consider this as fixed as soon the sample project I created starts working just by updating the referenced nuget packages to the fixed versions.

I think the most appropiate people I should mention here are: @ryancyq @acjh and @hikalkan

Best regards, Emiliano.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
acjhcommented, Dec 14, 2017

Thanks, I was able to reproduce that.

0reactions
acjhcommented, Jan 2, 2018

Fixed by #2841

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Error with lazy loading in Entity Framework Entity
I'd say that the problem here is that in your query you are not explicitly loading the related entities (in this case, ServiceStatusHistory....
Read more >
Lazy Loading Related Data In Entity Framework Core
Lazy loading of data is a pattern whereby the retrieval of data from the database is deferred until it is needed.
Read more >
Lazy Loading of Related Data - EF Core
Lazy loading of related data with Entity Framework Core. ... The simplest way to use lazy-loading is by installing the Microsoft.
Read more >
Avoid Lazy Loading in ASP.NET - Shawn Wildermuth
Luckily, Entity Framework Core has made it harder to inadvertently turn it on. Let's see what's wrong with Lazy Loading in Web Apps....
Read more >
How to build application using the already existing ...
I want to use the existing database tables, SPs, and triggers also. Is there any way to generate the migration, dbcontext, and snapshot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found