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

Support for ValueGeneration OnUpdate #12611

Closed
smitpatel opened this issue Jul 9, 2018 · 3 comments
Closed

Support for ValueGeneration OnUpdate #12611

smitpatel opened this issue Jul 9, 2018 · 3 comments

Comments

@smitpatel
Copy link
Contributor

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace EFSampleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var blog = new Blog { Value = "A" };
            using (var db = new MyContext())
            {
                // Recreate database
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();

                // Seed database
                var entry = db.Add(blog);
                Console.WriteLine(entry.CurrentValues["Discriminator"]);

                db.SaveChanges();
            }

            using (var db = new MyContext())
            {
                blog.Value = "B";
                var entry = db.Update(blog);
                Console.WriteLine(entry.CurrentValues["Discriminator"]);
                db.SaveChanges();
            }

            Console.WriteLine("Program finished.");
        }
    }


    public class MyContext : DbContext
    {
        private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);

        // Declare DBSets
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<SpecialBlog> SpecialBlogs { 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")
                //.UseSqlite("filename=_modelApp.db")
                //.UseInMemoryDatabase(databaseName: "_modelApp")
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(LoggerFactory);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Configure model
        }
    }

    public class Blog
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

    public class SpecialBlog : Blog
    {
    }
}

The "Discriminator" property on entity entry is set to correct value when doing Add but when doing Update the value is null. For relational it works since update does not change values in database due to partial update. Though the value is required for CosmosDb provider since there are not partial updates.

@divega
Copy link
Contributor

divega commented Jul 11, 2018

This seems important for Cosmos DB, stored procedures and general client generated values for update could potentially could help with optimistic "offline lock".

@divega
Copy link
Contributor

divega commented Jul 11, 2018

For the time being we have a workaround this for Cosmos DB.

@AndriySvyryd
Copy link
Member

Duplicate of #6999

@AndriySvyryd AndriySvyryd marked this as a duplicate of #6999 Dec 7, 2018
@AndriySvyryd AndriySvyryd removed this from the Backlog milestone Oct 20, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 21, 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

4 participants