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

bug or question for cosmos db provider about replacing an object that throw an error on a property id that doesnt exist #28730

Closed
Spirch opened this issue Aug 15, 2022 · 1 comment

Comments

@Spirch
Copy link

Spirch commented Aug 15, 2022

This might be a bug or a configuration mistake on my part

Running the code below, I was expecting the Bug method to work but instead it is giving me this error

The property 'GrandChild.Id' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.

But if I run the Work method, it work as expected.

I compared the change tracker in both scenario, the difference is for the Bug one the new child get added and the old one get deleted while the Work one leave it as Unmodified, everything else id identical.

using Microsoft.EntityFrameworkCore;

namespace comos_id_bug
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Setup();
            Bug();
            Work();

            Console.WriteLine("Bye!");
            Console.Read();
        }

        static void Setup()
        {
            using BugContext context = new BugContext();

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.Root.Add(new Root()
            {
                Child = new Child()
                {
                    GrandChildren = new List<GrandChild>()
                    {
                        new GrandChild() { Data = "abc"},
                        new GrandChild() { Data = "def"},
                    },
                }
            });

            context.SaveChanges();

            Console.WriteLine("setup done!");
        }

        static Child newData => new Child()
        {
            GrandChildren = new List<GrandChild>()
            {
                        new GrandChild() { Data = "foo"},
                        new GrandChild() { Data = "bar"},
            }
        };
 
        static void Bug()
        {
            try
            {
                using BugContext context = new BugContext();

                var current = context.Root.SingleOrDefault();

                current.Child = newData;

                context.SaveChanges();

                Console.WriteLine("bug, worked ?!");
            }
            catch(Exception ex)
            {
                Console.WriteLine("Bug!");
                Console.WriteLine(ex.Message);
            }
        }

        static void Work()
        {
            using BugContext context = new BugContext();

            var current = context.Root.SingleOrDefault();

            current.Child.GrandChildren = newData.GrandChildren;

            context.SaveChanges();

            Console.WriteLine("work, worked!");
        }
    }

    class Root
    {
        public Guid Id { get; set; }
        public Child Child { get; set; }
    }

    class Child
    {
        public List<GrandChild> GrandChildren { get; set; }
    }

    class GrandChild
    {
        public string Data { get; set; }
    }

    class BugContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseCosmos(

        "https://localhost:8081",

        "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",

        databaseName: "BugId");

        public DbSet<Root> Root { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultContainer("Bug");

            modelBuilder.Entity<Root>();

            modelBuilder.Entity<Root>().Property(x => x.Id).ToJsonProperty("id");
        }
    }
}

Include provider and version information

Latest version of everything (net6) as of this post created date

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Aug 16, 2022

Duplicate of #27918

@AndriySvyryd AndriySvyryd marked this as a duplicate of #28600 Aug 16, 2022
@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2022
@AndriySvyryd AndriySvyryd marked this as a duplicate of #27918 Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants