Entity Framework not working with temporal table

后端 未结 5 1851
北海茫月
北海茫月 2020-12-03 07:37

I\'m using database first entity framework 6. After changing some of the tables in my schema to be temporal tables, I started getting the following error when attempting to

5条回答
  •  情书的邮戳
    2020-12-03 08:04

    There are two solutions to this problem:

    1. In the property window for the column in the EDMX designer, change the StoreGeneratedPattern on the PERIOD columns (ValidFrom and ValidTo in my case) to be identity. Identity is better than computed since computed will cause EF to refresh the values on an Insert and Update as opposed to just an insert with identity
    2. Create an IDbCommandTreeInterceptor implementation to remove the period columns. This is my preferred solution since it requires no additional work when adding new tables to the model.

    Here's my implementation:

    using System.Data.Entity.Infrastructure.Interception; 
    using System.Data.Entity.Core.Common.CommandTrees; 
    using System.Data.Entity.Core.Metadata.Edm; 
    using System.Collections.ObjectModel;
    
    internal class TemporalTableCommandTreeInterceptor : IDbCommandTreeInterceptor
    {
        private static readonly List _namesToIgnore = new List { "ValidFrom", "ValidTo" };
    
        public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
        {
            if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace)
            {
                var insertCommand = interceptionContext.Result as DbInsertCommandTree;
                if (insertCommand != null)
                {
                    var newSetClauses = GenerateSetClauses(insertCommand.SetClauses);
    
                    var newCommand = new DbInsertCommandTree(
                        insertCommand.MetadataWorkspace,
                        insertCommand.DataSpace,
                        insertCommand.Target,
                        newSetClauses,
                        insertCommand.Returning);
    
                    interceptionContext.Result = newCommand;
                }
    
                var updateCommand = interceptionContext.Result as DbUpdateCommandTree;
                if (updateCommand != null)
                {
                    var newSetClauses = GenerateSetClauses(updateCommand.SetClauses);
    
                    var newCommand = new DbUpdateCommandTree(
                        updateCommand.MetadataWorkspace,
                        updateCommand.DataSpace,
                        updateCommand.Target,
                        updateCommand.Predicate,
                        newSetClauses,
                        updateCommand.Returning);
    
                    interceptionContext.Result = newCommand;
                }
            }
        }
    
        private static ReadOnlyCollection GenerateSetClauses(IList modificationClauses)
        {
            var props = new List(modificationClauses);
            props = props.Where(_ => !_namesToIgnore.Contains((((_ as DbSetClause)?.Property as DbPropertyExpression)?.Property as EdmProperty)?.Name)).ToList();
    
            var newSetClauses = new ReadOnlyCollection(props);
            return newSetClauses;
        }
    }
    

    Register this interceptor with EF by running the following anywhere in your code before you use your context:

    DbInterception.Add(new TemporalTableCommandTreeInterceptor());
    

提交回复
热议问题