-
Notifications
You must be signed in to change notification settings - Fork 58
Description
Hello,
I don't know if this is an issue or I am doing something wrong as I have not been able to locate any example or documentation about this.
I am trying to use BulkInsert in net core 9 and entity framework 9 with property bags as Dictionary<string,object> and shared types.
An example would be the following:
Assume you have a SQL Server table:
sql:
CREATE TABLE Settings (
Id INT PRIMARY KEY,
[Key] NVARCHAR(100),
[Value] NVARCHAR(100)
);
Class:
public class AppDbContext : DbContext
{
public DbSet<Dictionary<string, object>> Settings => Set<Dictionary<string, object>>("Settings");
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Your_Connection_String_Here");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.SharedEntityType<Dictionary<string, object>>("Settings", b =>
{
b.Property<int>("Id");
b.Property<string>("Key");
b.Property<string>("Value");
b.HasKey("Id");
// Optional: If there are triggers, use this to avoid OUTPUT clause issues
b.ToTable("Settings", t => t.HasTrigger("tr_Settings_InsertOrUpdate"));
});
}
}
Perform Bulk Insert:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var settingsToInsert = new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
["Id"] = 1,
["Key"] = "SiteTitle",
["Value"] = "MyApp"
},
new Dictionary<string, object>
{
["Id"] = 2,
["Key"] = "Theme",
["Value"] = "Dark"
}
};
using var context = new AppDbContext();
// Bulk insert into the Settings table
context.BulkInsert(settingsToInsert, options =>
{
options.DestinationTableName = "Settings";
});
Console.WriteLine("Bulk insert completed.");
}
}
But, when I execute i am getting the following exception:
Message:
System.Exception : Oops! The Entity Type could not be found for the type 'System.Collections.Generic.Dictionary2[System.String,System.Object]' or 'System.Collections.Generic.Dictionary2[System.String,System.Object]'.
Stack Trace:
.[](DbContext this, IEnumerable1 , List1 )
.BulkInsert[T](DbContext this, IEnumerable1 entities, Action1 options, Boolean isBulkSaveChanges, Boolean isOptimized)
DbContextExtensions.BulkInsert[T](DbContext this, IEnumerable1 entities, Action1 options)
1.() ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location --- ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location --- .[](DbContext , Func2 , Action`1 , CancellationToken )
Are shared types and property bags supported?