We are running ABP MVC 5/ JQuery and upgraded today to 3.6.1. This code:
var tags = await _departmentRepository
.GetAllIncluding(x => x.OrganizationUnit)
.GroupJoin(_userOrganizationUnitRepository.GetAll(),
t => t.OrganizationUnitId,
a => a.OrganizationUnitId,
(t, a) => new { Assignments = a, Tag = t }
)
.Where(x => x.Tag.OrganizationUnit.ParentId == parentId)
.ToListAsync();
now throws: "The specified LINQ expression contains references to queries that are associated with different contexts." This error is occurring at run-time for multiple LINQ expressions similar to the one above. Do we need to change our code? Do you know what could be causing this?
Thank you for your time.
13 Answer(s)
-
0
@careLearning what was your previous ABP version ?
-
0
Entity Framework does not allow having two different database contexts in a single query.
You have to seperate your queries see
- <a class="postlink" href="https://stackoverflow.com/questions/7332920/error-the-specified-linq-expression-contains-references-to-queries-that-are-as">https://stackoverflow.com/questions/733 ... hat-are-as</a>
- <a class="postlink" href="https://stackoverflow.com/questions/47489840/error-the-specified-linq-expression-contains-references-to-queries-that-are-as">https://stackoverflow.com/questions/474 ... hat-are-as</a>
I don't think this is related to the AspNet Zero Update. Revert back your code and see if it's still working.
-
0
@ismcagda It looks like we gave you the version according to ABP Packages and not the product version. Sorry. We went from 5.3.0.0 to 5.4.0.0. Which means we were using the ABP 3.5.0.0 packages from this commit 9f6723952946c5f3faedfdbb079d318d34e7b6a3; but after merging commit 8cf59103b5d0c014f6604042b4ce5ed7246c1f03 it now includes the ABP 3.6.1.0 packages.
@alper When we roll back to our previous commit everything works as expected.
We are using the Single Deployment - Multiple Database Strategy ( <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy">https://aspnetboilerplate.com/Pages/Doc ... ti-Tenancy</a> ).
We have added two custom properties to an organization unit (locked and customKey) to our Department entity:
namespace MyCompanyName.AbpZeroTemplate.Departments { using OrganizationUnits; public class Department : OrganizationUnitMap { public const int MaximumIdLength = 50; public const int MaximumTitleLength = 50; public string CustomKey { get; set; } public bool Locked { get; set; } public Department() { } public Department(long organizationUnitId, string cutomKey, bool locked = false) { CustomKey = cutomKey; Locked = locked; OrganizationUnitId = organizationUnitId; } } }
The query we wrote joins this entity via the OrganizationUnit navigation property to the assignments table (AbpUserOrganizationUnits) so we can determine which users have which departments. All of these entities should belong to the same TenantDB context.
Tenant Context:
public class AbpZeroTemplateTenantDbContext : AbpZeroTenantDbContext<Role, User> { // removed other dbsets public virtual IDbSet<Department> Departments { get; set; } #region Constructors public AbpZeroTemplateTenantDbContext() : base("Tenant") { } public AbpZeroTemplateTenantDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public AbpZeroTemplateTenantDbContext(DbConnection existingConnection) : base(existingConnection, false) { } public AbpZeroTemplateTenantDbContext(DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection) { } #endregion protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // removed other configurations modelBuilder.Configurations.Add(new DepartmentConfiguration()); } }
Host Context
[DefaultDbContext] public class AbpZeroTemplateHostDbContext : AbpZeroHostDbContext<Tenant, Role, User> { /* Define an IDbSet for each entity of the application */ public virtual IDbSet<BinaryObject> BinaryObjects { get; set; } public virtual IDbSet<Friendship> Friendships { get; set; } public virtual IDbSet<ChatMessage> ChatMessages { get; set; } public virtual IDbSet<EmailAddressDomain> EmailAddressDomains { get; set; } public virtual IDbSet<FileType> FileTypes { get; set; } public virtual IDbSet<TenantUsername> TenantUsernames { get; set; } public AbpZeroTemplateHostDbContext() : base("Host") { } public AbpZeroTemplateHostDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public AbpZeroTemplateHostDbContext(DbConnection existingConnection) : base(existingConnection, false) { } public AbpZeroTemplateHostDbContext(DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.Add(new EmailAddressDomainConfiguration()); modelBuilder.Configurations.Add(new FileTypeConfiguration()); modelBuilder.Configurations.Add(new TenantUsernameConfiguration()); modelBuilder.Configurations.Add(new UserConfiguration()); } }
-
0
Hi,
I think your problem is related to <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/pull/3249/files">https://github.com/aspnetboilerplate/as ... 3249/files</a>. Please continue with ABP 3.5 for now, we will try to understand and fix this if there is a problem.
-
0
@ismcagdas Good eye!
@careLearning [DefaultDbContext] attribute was wrong before. But you might not need it. Can you try the following?
[MultiTenancySide(MultiTenancySides.Host)] public class AbpZeroTemplateHostDbContext : AbpZeroHostDbContext<Tenant, Role, User>
-
0
@aaron thanks, not as good as yours ;)
-
0
@ismcagdas Thank you for looking into this
@aaron Thanks for your response. When we add:
[MultiTenancySide(MultiTenancySides.Host)]
to our host file and
[MultiTenancySide(MultiTenancySides.Tenant)]
to our tenant file we get this error now instead of the error above: The specified cast from a materialized 'System.Data.Entity.Core.Objects.MaterializedDataRecord' type to the '<>f__AnonymousType16`2[MyCompanyName.AbpZeroTemplate.Departments.Department,Abp.Authorization.Users.UserOrganizationUnit]' type is not valid.
We also noticed that this is not just on groupJoin statements, but regular join statements. Any thoughts or suggestions would be greatly appreciated.
Thanks again.
-
0
@careLearning could you share your project via email ?
-
0
Hello,
We have created a sample project and sent an email to your Volosoft's "info" email account. This email contains instructions for how to download the solution that will illustrate the problem. Please let us know if you have any trouble or concerns.
Thank you.
-
0
@ismcagdas We wanted to check with you to see if you received the email. Were you able to download the solution and run it?
Thanks again.
-
0
Hi @careLearning,
Thank you for sharing the project with us. The application throws "The specified LINQ expression contains references to queries that are associated with different contexts." exception.
but after adding attributes to related DbContexts, it worked for us.
[DefaultDbContext] [MultiTenancySide(MultiTenancySides.Host)] public class AbpZeroTemplateHostDbContext : AbpZeroHostDbContext<Tenant, Role, User>
[MultiTenancySide(MultiTenancySides.Tenant)] public class AbpZeroTemplateTenantDbContext : AbpZeroTenantDbContext<Role, User>
Also, removing DefaultDbContext from host DbContext didn't make any change.
-
0
Dear @ismcagdas ,
Sorry for the confusion. We tested it again and those changes are working as you stated they would.
Thanks for the solution.
-
0
@careLearning that is great :)