-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EF Core 3.1.7 Eager Load two level ThenInclude last one with inherit type not working #22380
Comments
|
@smitpatel it's the intermediate between Person and SecurityUser to avoid circular reference as each live on a different project/DLL, so CrossSecurityUser inherit SecurityUser and add one-to-one to Person. |
@alfred-zaki - That is all fine but where in EF Core model are you registering it as an entity type? You also need to add it to EF as an entity type. |
@smitpatel are you referring to IEntityTypeConfiguration ? i either get that this need a PrimaryKey or i need to set it to HasNoKey which is not the case, or i get Invalid Column Name Discriminator ! this is basically the same exact table on SQL level |
Yes, there is no IEntityTypeConfiguration for |
@smitpatel ok so i guess i was on the right track and drafted as i encountered a problem, if you please can guide on how to do that discriminator, if i am getting this correctly the difference between the two models are in CrossSecurityUser PersonId is not null so on SQL level i have SecurityUser table which have a a Guid? PersonId column, if you have a value there that mean this security user have Person record with this Id, if not then no record, can you please help me express that or guide me on what to read. |
@smitpatel will hit the bed now, thanks for all your help. |
#10140 is needed to map this scenario correctly |
@smitpatel @AndriySvyryd thanks for responding!, so let me ask this in a different way, how can i have two classes in EF that map to the same table with one being the base class and the other being the same just with a couple of extra columns? what if we want each to live on a different project/dll? |
@alfred-zaki - Without #10140, it is not possible unless you also add discriminator column to your table. Though, I am not able to understand, how are the classes in first 2 DLLs are related to each other, why the second DLL not reference first one? The code you have shared has no multiple circular dependency issue. |
@smitpatel yes the code doesn't cause i removed it by having that 3rd project/dll solution. but originally what i wanted to achieve was simple, an optional one-to-one relationship between "Person" and "SecurityUser" each is living on a different DLL, to do that from both sides i need for project1 to reference project2 to use SecurityUser in Person model class and IEntityTypeConfiguration and the other way around, of course that will trigger circular reference and VS prevent it. so i created a third project DLL and referenced both projects in it, started to build the "CorssSecurityUser" which is "SecurityUser" plus the foreign key column and ..... here i am |
See https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities The classes would look something like public class SecurityUser
{
public Guid SecurityUserId { get; set; }
[Required]
public string UserName { get; set; }
[NotMapped]
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public string Hash { get; set; }
public string Salt { get; set; }
public byte[] RowVersion { get; set; }
public virtual List<SecurityGroupsUsers> SecurityGroups { get; set; }
public virtual CrossSecurityUser CrossSecurityUser { get; set; }
}
public class CrossSecurityUser
{
public Guid SecurityUserId { get; set; }
public Guid? PersonId { get; set; }
[ForeignKey("PersonId")]
public virtual Person Person { get; set; }
} And you'd configure it with |
@AndriySvyryd Thank you for your suggestion, but it seems i will face the same circular reference situation if i want to do this from both sides. |
@alfred-zaki We discussed this but we don't have any further advice to give, and we don't see anything actionable on the EF side. In the future, shadow navigations might help. |
@ajcvickers Thanks for your feedback and consideration. |
@alfred-zaki Please share the link if you think it will help others. |
here's my solution to the issue i encountered, hope it's useful to someone. |
second .ThenInclude with a derived type class, .ThenInclude(....).ThenInclude(x => ((derivedClass)x).prop)
i have below code not working and need help to make it work.
i have two data models classes in two separate DLLs, and wanted to create a one-to-one optional relationship, of course with direct reference that would lead to "circular reference" situation.
my solution was to have a third DLL referencing both and have another data model inherited from one of the models and add relationship to that.
now i need to include the extra property on that module, my code goes as follow
on 1st DLL, Person Module
on 2nd DLL, SecurityGroup, SecurityGroupUsers and Security User which i want one-to-one with Person and same from Person (Circular reference) Modules
to avoid that i have a 3rd DLL with
now when this execute
i get "Invalid include."
Stack Trace
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.PopulateIncludeTree(IncludeTreeNode includeTreeNode, Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessInclude(NavigationExpansionExpression source, Expression expression, Boolean thenInclude)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0
1.<ExecuteAsync>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func
1 compiler)at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func
1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable
1 source, Expression expression, CancellationToken cancellationToken)at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable
1 source, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync[TSource](IQueryable
1 source, CancellationToken cancellationToken)at WorkNxt.Security.Api.Controllers.GroupController.d__7.MoveNext() in {removed for privacy}\WorkNxt.Security.Api\Controllers\GroupController.cs:line 101
Further technical details
EF Core version: 3.1.7
Database provider: (Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (.NET Core 3.1)
Operating system: Windows10
IDE: (Visual Studio 2019 16.7.2)
The text was updated successfully, but these errors were encountered: