-
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
Set operations need to assign correct nullability to shaper #19253
Comments
@LostAsk When I run this code I get the following exception, which makes sense given the model defined and the way it is used. It is not the exception you are reporting.
|
@ajcvickers
|
The query results in creation of entity instances which will then be tracked. However, if any of those instances are null then we are still trying to track them when we should not. @LostAsk Using Results look like: Simplified repro: namespace AbpEfCore
{
public class JoinResult<TLeft, TRight>
{
public TLeft Left { get; set; }
public TRight Right { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
using (var db = new MyContext())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
var tmp_a = new A[]
{
new A {a = "a0", a1 = "a1", forkey = "a"},
new A {a = "a2", a1 = "a1", forkey = "d"},
};
var tmp_b = new B[]
{
new B {b = "b0", b1 = "b1", forkey = "a"},
new B {b = "b2", b1 = "b1", forkey = "c"},
};
db.A.AddRange(tmp_a);
db.B.AddRange(tmp_b);
db.SaveChanges();
}
using (var db = new MyContext())
{
// Run queries
Expression<Func<A, string>> leftKeySelector = x => x.forkey;
Expression<Func<B, string>> rightKeySelector = y => y.forkey;
var query = db.A.AsNoTracking().GroupJoin(
db.B,
leftKeySelector,
rightKeySelector,
(left, rightg) => new
{
left,
rightg
})
.SelectMany(
r => r.rightg.DefaultIfEmpty(),
(x, y) => new JoinResult<A, B>
{
Left = x.left,
Right = y
})
.Concat(
db.B.GroupJoin(
db.A,
rightKeySelector,
leftKeySelector,
(right, leftg) => new {leftg, right})
.SelectMany(l => l.leftg.DefaultIfEmpty(),
(x, y) => new JoinResult<A, B>
{
Left = y,
Right = x.right
})
.Where(z => z.Left.Equals(null)))
.ToList();
}
}
}
public class MyContext : DbContext
{
private static ILoggerFactory ContextLoggerFactory
=> LoggerFactory.Create(b =>
{
b
.AddConsole()
.AddFilter("", LogLevel.Debug);
});
public DbSet<A> A { get; set; }
public DbSet<B> B { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Select 1 provider
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
.EnableSensitiveDataLogging()
.UseLoggerFactory(ContextLoggerFactory);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure model
}
}
public class A
{
public int Id { get; set; }
public string a { get; set; }
public string a1 { get; set; }
public string forkey { get; set; }
}
public class B
{
public int Id { get; set; }
public string b { get; set; }
public string b1 { get; set; }
public string forkey { get; set; }
}
} |
Set operation did not assign proper nullability to entity shaper. |
OK! Thanks! |
@ajcvickers @smitpatel
Throw Exception:
|
Also improved In-Memory set operation implementation Resolves #19253
Throw:
System.InvalidOperationException:“Unable to track an entity of type 'A' because primary key property 'Id' is null.”
The text was updated successfully, but these errors were encountered: