Skip to content

Sqlite json column names are being ignored #639

@lara-ec

Description

@lara-ec

Hi,

With Sqlite, it doesn't seem possible to define the column names of json columns when using BulkMerge. It seems to always just assume that the column is called nameof(ColumnName):

Unhandled exception. System.Exception: An error occured while resolving mapping by name. See the inner exception for details
 ---> System.Exception: Missing Column : DataObject
On entity : Table
On Table : "table"
   at Z.BulkOperations.BulkOperation.(String , Boolean , Boolean , Boolean )
   at Z.BulkOperations.BulkOperation.()
   --- End of inner exception stack trace ---
   at Z.BulkOperations.BulkOperation.()
   at Z.BulkOperations.BulkOperation.Execute()
   at Z.BulkOperations.BulkOperation.BulkMerge()
   at .BulkMerge[T](DbContext this, IEntityType entityType, IEnumerable`1 list, Action`1 options, SavingSelector savingSelector, Boolean forceSpecificTypeMapping)
   at .BulkMerge[T](DbContext this, IEnumerable`1 entities, Action`1 options, Boolean isBulkSaveChanges)
   at DbContextExtensions.BulkMerge[T](DbContext this, IEnumerable`1 entities, Action`1 options)
   at DbContextExtensions.`1.()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at .[](DbContext , Func`2 , Action`1 , CancellationToken )
   at SqliteJson.Program.Main() in /tmp/tmp.1NCNdjWeCh/SqliteJson/Program.cs:line 43
   at SqliteJson.Program.<Main>()

Here the example code:

using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace SqliteJson
{
    public class Entity;

    [Table("table")]
    public class Table
    {
        [Column("id")]
        public int Id { get; set; }

        [Column("data", TypeName = "json")]
        public required Entity DataObject { get; set; }
    }

    public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options)
    {
        public DbSet<Table> Table { get; set; } = null!;

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Table>(property =>
                property.OwnsOne(x => x.DataObject).ToJson("data")
            );
        }
    }

    public static class Program
    {
        public static async Task Main()
        {
            var connection = new SqliteConnection("DataSource=:memory:");
            connection.Open();

            var context = new MyDbContext(
                new DbContextOptionsBuilder<MyDbContext>().UseSqlite(connection).Options
            );

            await context.Database.EnsureCreatedAsync();
            await context.Table.BulkMergeAsync([new Table { DataObject = new Entity() }]);
        }
    }
}

SqliteJson.zip

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions