You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Post with nullBlog can be saved using the in-memory provider even if the Post.BlogId is non-nullable. But when a Post is saved with a Blog instance and then later changed to null, the said Post entity will be implicitly deleted.
Steps to reproduce
using Microsoft.EntityFrameworkCore;using System;using System.Linq;using Xunit;publicclassTests{privatereadonlyDbContextOptions<Context>_options;publicTests(){_options=newDbContextOptionsBuilder<Context>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;}[Fact]publicvoidCan_save_Post_with_null_Blog(){using(varctx=new Context(_options)){
ctx.Add(new Post {Blog=null});
ctx.SaveChanges();}using(varctx=new Context(_options)){
Assert.NotEmpty(ctx.Post);}}[Fact]publicvoidIn_memory_implicitly_deletes_Post_when_Blog_is_set_to_null(){using(varctx=new Context(_options)){
ctx.Add(new Post {Blog=new Blog()});
ctx.SaveChanges();}using(varctx=new Context(_options)){
ctx.Post.Include(p => p.Blog).First().Blog =null;
ctx.SaveChanges();}using(varctx=new Context(_options)){
Assert.NotEmpty(ctx.Post);}}}publicclassContext:DbContext{publicContext(DbContextOptions<Context>options):base(options){}publicDbSet<Post> Post {get;set;}publicDbSet<Blog> Blog {get;set;}}publicclassPost{publicintPostId{get;set;}publicintBlogId{get;set;}publicBlogBlog{get;set;}}publicclassBlog{publicintBlogId{get;set;}}
Further technical details
EF Core version: 2.1.3
Database Provider: Microsoft.EntityFrameworkCore.InMemory
Operating system: Win7
IDE: Visual Studio 2017 15.4
The text was updated successfully, but these errors were encountered:
@gojanpaolo For the first case, the save is not throwing because the in-memory provider currently doesn't enforce relational constraints--see #2166.
For the second case, Post.BlogId is not nullable, which implies a required relationship between Blog and Post. This means that when the relationship is severed the default behavior is to delete the dependent (Post) since it can't exist without it's parent. See https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete
A
Post
withnull
Blog
can be saved using the in-memory provider even if thePost.BlogId
is non-nullable. But when aPost
is saved with aBlog
instance and then later changed tonull
, the saidPost
entity will be implicitly deleted.Steps to reproduce
Further technical details
EF Core version: 2.1.3
Database Provider: Microsoft.EntityFrameworkCore.InMemory
Operating system: Win7
IDE: Visual Studio 2017 15.4
The text was updated successfully, but these errors were encountered: