コード例 #1
0
        public void BulkInsertOrUpdate_EntityWithShadowProperties_SavesToDatabase(DbServer databaseType)
        {
            ContextUtil.DbServer = databaseType;

            using var db = new SpDbContext(ContextUtil.GetOptions <SpDbContext>(databaseName: $"{nameof(EFCoreBulkTest)}_ShadowProperties"));

            db.BulkInsertOrUpdate(this.GetTestData(db).ToList(), new BulkConfig
            {
                EnableShadowProperties = true
            });

            var modelFromDb = db.SpModels.OrderByDescending(y => y.Id).First();

            Assert.Equal((long)10, db.Entry(modelFromDb).Property(SpModel.SpLong).CurrentValue);
            Assert.Null(db.Entry(modelFromDb).Property(SpModel.SpNullableLong).CurrentValue);

            Assert.Equal(new DateTime(2021, 02, 14), db.Entry(modelFromDb).Property(SpModel.SpDateTime).CurrentValue);
        }
コード例 #2
0
        public void BulkInsertOrUpdate_EntityWithShadowProperties_GlobalFunc_SavesToDatabase(DbServer dbServer)
        {
            ContextUtil.DbServer = dbServer;

            using var db = new SpDbContext(ContextUtil.GetOptions <SpDbContext>(databaseName: $"{nameof(EFCoreBulkTest)}_ShadowProperties"));

            var data = GetTestData(db, false, 10000);

            db.BulkInsertOrUpdate(data.ToList(), new BulkConfig
            {
                EnableShadowProperties = true,
                ShadowPropertyValue    = (entity, property) =>
                {
                    if (property == SpModel.SpLong)
                    {
                        return(10);
                    }
                    else if (property == SpModel.SpNullableLong)
                    {
                        return(null);
                    }
                    else if (property == SpModel.SpDateTime)
                    {
                        return(new DateTime(2021, 02, 14));
                    }

                    return(property);
                }
            });

            var modelFromDb = db.SpModels.OrderByDescending(y => y.Id).First();

            Assert.Equal((long)10, db.Entry(modelFromDb).Property(SpModel.SpLong).CurrentValue);
            Assert.Null(db.Entry(modelFromDb).Property(SpModel.SpNullableLong).CurrentValue);

            Assert.Equal(new DateTime(2021, 02, 14), db.Entry(modelFromDb).Property(SpModel.SpDateTime).CurrentValue);
        }
コード例 #3
0
 public void Dispose()
 {
     using var db = new SpDbContext(ContextUtil.GetOptions <SpDbContext>(databaseName: $"{nameof(EFCoreBulkTest)}_ShadowProperties"));
     db.Database.EnsureDeleted();
 }