You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In our case, we want to insert a list of Vehicle objects, where the elements can be either Vehicle, Car or Bus
However, when attempting to use applicationDbContext.BulkInsertAsync(elements), we encountered the following error :
Using 'FromSqlRaw' on DbSet of 'Vehicle' is not supported since 'Vehicle' is part of hierarchy and does not contain a discriminator property.
To resolve this, we added a shadow property Discriminator to all entities in ApplicationDContext
System.AggregateException: One or more errors occurred. (Invalid column name 'Capacity'.
Invalid column name 'NumberOfSeats'. Invalid column name 'Horsepower'.) ---> Microsoft.Data.SqlClient.SqlException: Invalid column name 'Capacity'.
Invalid column name 'NumberOfSeats'. Invalid column name 'Horsepower'
The same error occurs when using TPH (e.g. removing the .ToTable lines from ApplicationDContext)
Is there any way to successfully use BulkInsert on a list in this case?
Thanks in advance for your assistance.
The text was updated successfully, but these errors were encountered:
Hello @borisdj,
We are really stuck with this issue.
Is there a way, even locally, to work around this issue and insert a list containing elements from different hierarchy levels ?
To complete our error case, here is the specific use that is failing.
First you do should not call SaveChangesAsync when using BulkMethods.
Secondly you need to have 'Discriminator' column set.
One way would be to expose that property and set it to appropriate table name for each object before calling BulkMethod.
And another option, if you want it to remain Shadow property, than you need first to add list into DbSet with AddRange, which will set Discr.col. automatically, and after that call BulkInsertAsync only (do Not call SaveChangesAsync at the end)
You can take at DiscriminatorShadowTest for sample:
Hello
We encountered an issue when using TPC inheritance with entities (with SQLServer)
We have entities like this
In our case, we want to insert a list of
Vehicle
objects, where the elements can be eitherVehicle
,Car
orBus
However, when attempting to use
applicationDbContext.BulkInsertAsync(elements)
, we encountered the following error :To resolve this, we added a shadow property
Discriminator
to all entities inApplicationDContext
However, this resulted in a new error:
The same error occurs when using TPH (e.g. removing the
.ToTable
lines fromApplicationDContext
)Is there any way to successfully use
BulkInsert
on a list in this case?Thanks in advance for your assistance.
The text was updated successfully, but these errors were encountered: