提交 8d6f2372 编写于 作者: B Boris Djurdjevic

changes

上级 9b28a873
using EFCore.BulkExtensions.SqlAdapters;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using NetTopologySuite.Geometries;
using System;
using System.Collections.Generic;
......@@ -640,6 +641,7 @@ public class EFCoreBulkTestAtypical
[Theory]
[InlineData(SqlType.SqlServer)]
[InlineData(SqlType.PostgreSql)]
[InlineData(SqlType.Sqlite)] //Not supported
private void ShadowFKPropertiesTest(SqlType sqlType) // with Foreign Key as Shadow Property
{
......@@ -658,7 +660,6 @@ public class EFCoreBulkTestAtypical
}
//context.BulkDelete(context.Items.ToList()); // On table with FK Truncate does not work
if (!context.Items.Any())
{
for (int i = 1; i <= 10; ++i)
......@@ -670,7 +671,7 @@ public class EFCoreBulkTestAtypical
Description = string.Concat("info ", Guid.NewGuid().ToString().AsSpan(0, 3)),
Quantity = i % 10,
Price = i / (i % 5 + 1),
TimeUpdated = DateTime.Now,
TimeUpdated = sqlType == SqlType.PostgreSql ? DateTime.UtcNow : DateTime.Now,
ItemHistories = new List<ItemHistory>()
};
......@@ -683,13 +684,22 @@ public class EFCoreBulkTestAtypical
var entities = new List<ItemLink>();
for (int i = 0; i < EntitiesNumber; i++)
{
entities.Add(new ItemLink
var itemLink = new ItemLink
{
ItemLinkId = 0,
Item = items[i % items.Count]
});
};
context.Entry(itemLink).Property("Data").CurrentValue = "aaa";
context.Entry(itemLink).Property("ItemId").CurrentValue = itemLink.Item.ItemId;
entities.Add(itemLink);
}
context.BulkInsert(entities);
context.ItemLinks.AddRange(entities);
context.SaveChanges();
//context.BulkInsert(entities);
List<ItemLink> links = context.ItemLinks.ToList();
Assert.True(links.Count > 0, "ItemLink row count");
......@@ -1388,4 +1398,41 @@ public class EFCoreBulkTestAtypical
context.BulkRead(list2);
Assert.Equal("At2", list2.FirstOrDefault()?.Name);
}
[Theory]
[InlineData(SqlType.PostgreSql)]
private void XminTest(SqlType sqlType)
{
ContextUtil.DatabaseType = sqlType;
using var context = new TestContext(ContextUtil.GetOptions());
context.Truncate<Partner>();
var list = new List<Partner>
{
new Partner { Name = "Aa1", FirstName = "Ab2" },
new Partner { Name = "Ba1", FirstName = "Bb2" }
};
//context.BulkInsert(list);
context.Partners.AddRange(list);
context.SaveChanges();
var first = context.Partners.FirstOrDefault();
var list2 = new List<Partner>
{
new Partner
{
Id = first?.Id ?? Guid.Empty
}
};
var bulkConfig = new BulkConfig
{
UpdateByProperties = new List<string> { nameof(Partner.Id) },
PropertiesToInclude = new List<string> { nameof(Partner.Id), nameof(Partner.Name) }
};
context.BulkRead(list2, bulkConfig);
}
}
......@@ -81,6 +81,8 @@ public class TestContext : DbContext
public DbSet<Author> Authors { get; set; } = null!;
public DbSet<Partner> Partners { get; set; } = null!;
public static bool UseTopologyPostgres { get; set; } = false;
public TestContext(DbContextOptions options) : base(options)
......@@ -140,6 +142,8 @@ public class TestContext : DbContext
modelBuilder.Entity<FilePG>().Ignore(p => p.Formats);
modelBuilder.Entity<ItemLink>().Property<string>("Data");
if (Database.IsSqlServer())
{
modelBuilder.Entity<Document>().Property(p => p.DocumentId).HasDefaultValueSql("NEWID()");
......@@ -213,6 +217,10 @@ public class TestContext : DbContext
modelBuilder.Entity<Box>().Property(p => p.ElementContent).HasColumnType("jsonb"); // with annotation not mapped since not used for others DBs
modelBuilder.Entity<Box>().Property(p => p.DocumentContent).HasColumnType("jsonb"); // with annotation not mapped since not used for others DBs
}
else
{
modelBuilder.Entity<Partner>().Ignore(p => p.RowVersion);
}
//modelBuilder.Entity<Modul>(buildAction => { buildAction.HasNoKey(); });
modelBuilder.Entity<Modul>().Property(et => et.Code).ValueGeneratedNever();
......@@ -976,3 +984,15 @@ public class AddressCD
public string Postcode { get; set; }
public string Country { get; set; }
}
public class Partner
{
public Guid Id { get; set; }
public string Name { get; set; } = null!;
public string? FirstName { get; set; }
[Timestamp]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[Column("xmin", TypeName = "xid")]
public uint RowVersion { get; set; }
}
......@@ -81,7 +81,7 @@ public class PostgreSqlAdapter : ISqlOperationsAdapter
continue;
}
var propertyValue = GetPropertyValue(tableInfo, propertyName, entity);
var propertyValue = GetPropertyValue(context, tableInfo, propertyName, entity);
var propertyColumnName = tableInfo.PropertyColumnNamesDict.GetValueOrDefault(propertyName, "");
var columnType = tableInfo.ColumnNamesTypesDict[propertyColumnName];
......@@ -167,18 +167,41 @@ public class PostgreSqlAdapter : ISqlOperationsAdapter
}
}
static object? GetPropertyValue<T>(TableInfo tableInfo, string propertyName, T entity)
static object? GetPropertyValue<T>(DbContext context, TableInfo tableInfo, string propertyName, T entity)
{
var propertyValue = default(object);
if (!tableInfo.FastPropertyDict.ContainsKey(propertyName.Replace('.', '_')) || entity is null)
{
var objectIdentifier = tableInfo.ObjectIdentifier;
var shadowPropertyColumnNamesDict = tableInfo.ColumnToPropertyDictionary
.Where(a => a.Value.IsShadowProperty()).ToDictionary(a => a.Key, a => a.Value.GetColumnName(objectIdentifier));
if (shadowPropertyColumnNamesDict.ContainsKey(propertyName))
{
if (tableInfo.BulkConfig.ShadowPropertyValue == null)
{
propertyValue = context.Entry(entity!).Property(propertyName).CurrentValue;
}
else
{
propertyValue = tableInfo.BulkConfig.ShadowPropertyValue(entity!, propertyName);
}
if (tableInfo.ConvertibleColumnConverterDict.ContainsKey(propertyName))
{
propertyValue = tableInfo.ConvertibleColumnConverterDict[propertyName].ConvertToProvider.Invoke(propertyValue);
}
return propertyValue;
}
return null;
}
object? propertyValue = entity;
//object? propertyValue = entity;
string fullPropertyName = string.Empty;
foreach (var entry in propertyName.AsSpan().Split("."))
{
if (propertyValue == null)
if (entity == null)
{
return null;
}
......@@ -192,7 +215,7 @@ public class PostgreSqlAdapter : ISqlOperationsAdapter
fullPropertyName = new string(entry.Token);
}
propertyValue = tableInfo.FastPropertyDict[fullPropertyName].Get(propertyValue);
propertyValue = tableInfo.FastPropertyDict[fullPropertyName].Get(entity);
}
return propertyValue;
......
......@@ -805,7 +805,7 @@ public class SqlServerAdapter : ISqlOperationsAdapter
}
}
if (tableInfo.BulkConfig.EnableShadowProperties)
if (tableInfo.BulkConfig.EnableShadowProperties) // TODO change for regular Shadow props to work even without this config
{
foreach (var shadowPropertyName in shadowPropertyColumnNamesDict.Keys)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册