Skip to content
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

ForeignKeyAttribute can't be overriden with explicit configuration #5695

Closed
xrkolovos opened this issue Jun 8, 2016 · 2 comments
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@xrkolovos
Copy link

xrkolovos commented Jun 8, 2016

my model (Steps to reproduce)

I have a model with an optional one-to-one relationship like this

public class Decision : RelativeEntity{
        public int? SessionSubjectId { get; set; }

        [ForeignKey("SessionSubjectId")]
        public virtual SessionSubject SessionSubject { get; set; }
}
public class SessionSubject : RelativeEntity {
        public int? DecisionId { get; set; }

        [ForeignKey("DecisionId")]
        public virtual Decision Decision { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder) {
    modelBuilder.Entity<Decision>()
               .HasOne(ss => ss.SessionSubject)
               .WithOne(s => s.Decision)
               .HasForeignKey<SessionSubject>(m => m.DecisionId)
               .IsRequired(false);
    modelBuilder.Entity<SessionSubject>()
               .HasOne(ss => ss.Decision)
               .WithOne(s => s.SessionSubject)
               .HasForeignKey<Decision>(m => m.SessionSubjectId)
               .IsRequired(false);
}

The issue

So when the EF gets initialized and the OnModelCreating method runs, I get this error

An unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: builder

Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)

If i comment the fluent api code, and then try to include one model from the other like this

Context.SessionSubjects.Include(x => x.Decision).FirstOrDefault();

I get this error

An unhandled exception occurred while processing the request.
InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.Expression`1[System.Func`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Decisions.Core.Model.Db.SessionSubject,System.Collections.Generic.IAsyncEnumerable`1[Decisions.Core.Model.Db.Decision]],Decisions.Core.Model.Db.Decision],System.Int32]]' to type 'System.Linq.Expressions.ConstantExpression'.

Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.QueryFlattener.Flatten(MethodCallExpression methodCallExpression)

Further technical details

EF Core version: 1.0.0-rc2-final
Operating system: win10
Visual Studio version: 2015

@smitpatel
Copy link
Contributor

The error for model building part,
based on the exception message, seems dupe of #5657
Can you post whole stacktrace?

@ntsekouras
Copy link

ntsekouras commented Jun 9, 2016

the whole stacktrace is this:

System.ArgumentNullException: Value cannot be null.

Parameter name: builder
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceReferenceBuilder..ctor(InternalRelationshipBuilder builder, EntityType declaringEntityType, EntityType relatedEntityType)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceReferenceBuilder`2..ctor(InternalRelationshipBuilder builder, EntityType declaringEntityType, EntityType relatedEntityType)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceNavigationBuilder`2.WithOne(Expression`1 reference)
   at Decisions.EntityFramework.DecisionsDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Projects\Decisions\src\src\Decisions.EntityFramework\DecisionsDbContext.cs:line 155
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext dbContext)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass14_0.<GetModel>b__0(Object k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)

   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()

   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()

   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()

   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_4(IServiceProvider p)

   at lambda_method(Closure , ServiceProvider )

   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)

   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)

   at Microsoft.EntityFrameworkCore.DbContext.get_Model()

   at Microsoft.EntityFrameworkCore.DbContext.Set[TEntity]()

   at EgritosGroup.EntityFramework.Repositories.EfRepositoryBase`3.get_Table()

   at EgritosGroup.EntityFramework.Repositories.EfRepositoryBase`3.GetAll()

   at EgritosGroup.Core.Domain.Repositories.EgfRepositoryBase`2.FirstOrDefault(TPrimaryKey id)

   at EgritosGroup.Core.Domain.Repositories.EgfRepositoryBase`2.Get(TPrimaryKey id)

   at Decisions.Bll.Sessions.SessionAppService.<Get>d__10.MoveNext() in C:\Projects\Decisions\src\src\Decisions.Bll\Sessions\SessionAppService.cs:line 101

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

   at Decisions.WebUi.Controllers.SessionsController.<GetSession>d__6.MoveNext() in C:\Projects\Decisions\src\src\Decisions.WebUi\Controllers\SessionsController.cs:line 78

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

   at Microsoft.AspNetCore.Mvc.Internal.ObjectMethodExecutor.<CastToObject>d__24`1.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionAsync>d__6.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

   at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeActionFilterAsync>d__41.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeAsync>d__32.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.<InvokeActionAsync>d__8.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.<Invoke>d__5.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

@bricelam bricelam added this to the 1.0.1 milestone Jun 10, 2016
@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 14, 2016
@AndriySvyryd AndriySvyryd changed the title Can't configure an optional one-to-one relationship ForeignKeyAttribute can't be overriden with explicit configuration Jul 14, 2016
@AndriySvyryd AndriySvyryd removed their assignment Jul 14, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

7 participants