public class Customer
{
public int Id { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int Id { get; set; }
public int? CustomerId { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().OneToMany(c => c.Orders).ForeignKey(k => k.CustomerId);
}
protected override void OnConfiguring(DbContextOptions options)
{
options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=IncludeOptionalNavigationBug;Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
public class Program
{
public void Main(string[] args)
{
using (var ctx = new MyContext())
{
ctx.Database.EnsureCreated();
var o1 = new Order();
var o2 = new Order();
var c1 = new Customer
{
Orders = new List<Order> { o1, o2 },
};
ctx.Customers.Add(c1);
ctx.Orders.Add(o1);
ctx.Orders.Add(o2);
ctx.SaveChanges();
}
using (var ctx = new MyContext())
{
ctx.Customers.Include(c => c.Orders).ToList();
}
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
throws:
Unhandled Exception: System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Nullable1[System.Int32]' and 'System.Int32'. at System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType binaryType, String opName, Expression left, Expression right, Boolean liftToNull) at System.Linq.Expressions.Expression.Equal(Expression left, Expression right) at Microsoft.Data.Entity.Relational.Query.RelationalQueryModelVisitor.IncludeCollection(IQuerySource querySource, Type resultType, LambdaExpression accessorLambda, INavigation navigation) in d:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 266 at Microsoft.Data.Entity.Relational.Query.RelationalQueryModelVisitor.IncludeNavigation(IQuerySource querySource, Type resultType, LambdaExpression accessorLambda, INavigation navigation) in d:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 102 at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel, Type resultType, ICollection1 queryAnnotations) in d:\k\EntityFramework\src\EntityFramework\Query\EntityQueryModelVisitor.cs:line 248
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) in d:\k\EntityFramework\src\EntityFramework\Query\EntityQueryModelVisitor.cs:line 107
at Microsoft.Data.Entity.Relational.RelationalDataStore.Query[TResult](QueryModel queryModel) in d:\k\EntityFramework\src\EntityFramework.Relational\RelationalDataStore.cs:line 89
at Microsoft.Data.Entity.Query.EntityQueryExecutor.ExecuteCollection[T](QueryModel queryModel) in d:\k\EntityFramework\src\EntityFramework\Query\EntityQueryExecutor.cs:line 83
at Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteCollectionQueryModel[T](QueryModel queryModel, IQueryExecutor executor)
at Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteQueryModel(
QueryModel queryModel, IQueryExecutor executor)
at Remotion.Linq.QueryModel.Execute(IQueryExecutor executor)
at Remotion.Linq.QueryProviderBase.Execute(Expression expression)
at Remotion.Linq.QueryProviderBase.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at Remotion.Linq.QueryableBase1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
Following code:
throws:
Unhandled Exception: System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Nullable
1[System.Int32]' and 'System.Int32'. at System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType binaryType, String opName, Expression left, Expression right, Boolean liftToNull) at System.Linq.Expressions.Expression.Equal(Expression left, Expression right) at Microsoft.Data.Entity.Relational.Query.RelationalQueryModelVisitor.IncludeCollection(IQuerySource querySource, Type resultType, LambdaExpression accessorLambda, INavigation navigation) in d:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 266 at Microsoft.Data.Entity.Relational.Query.RelationalQueryModelVisitor.IncludeNavigation(IQuerySource querySource, Type resultType, LambdaExpression accessorLambda, INavigation navigation) in d:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 102 at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel, Type resultType, ICollection
1 queryAnnotations) in d:\k\EntityFramework\src\EntityFramework\Query\EntityQueryModelVisitor.cs:line 248 at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) in d:\k\EntityFramework\src\EntityFramework\Query\EntityQueryModelVisitor.cs:line 107 at Microsoft.Data.Entity.Relational.RelationalDataStore.Query[TResult](QueryModel queryModel) in d:\k\EntityFramework\src\EntityFramework.Relational\RelationalDataStore.cs:line 89 at Microsoft.Data.Entity.Query.EntityQueryExecutor.ExecuteCollection[T](QueryModel queryModel) in d:\k\EntityFramework\src\EntityFramework\Query\EntityQueryExecutor.cs:line 83 at Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteCollectionQueryModel[T](QueryModel queryModel, IQueryExecutor executor) at Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteQueryModel( QueryModel queryModel, IQueryExecutor executor) at Remotion.Linq.QueryModel.Execute(IQueryExecutor executor) at Remotion.Linq.QueryProviderBase.Execute(Expression expression) at Remotion.Linq.QueryProviderBase.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at Remotion.Linq.QueryableBase1.GetEnumerator() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)