-
-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BulkInsert Failed to attempt to insert with custom column name #359
Comments
When you replace the |
I have the same issue. it works with savechanges. |
What version of the library are you using? If you revert to version 3.1.1, does it work? I think it's important to confirm that this isn't a regression that occurred between version 3.1.2 to the current 3.1.4, as these all were released in the past week. |
same issue on both versions |
@gustavopinho - Is your EF core model valid? If you have a foreign key against a Correct me if I'm wrong, but what you have now is basically attempting to map a Specifically, this is what you have:
For a proper foreign key relationship, this is what you are supposed to have. Technically I believe you don't need the attribute on both the navigation element and the foreign key itself, but for demonstration's sake:
|
Updated But my model is valid! Base example:
In my example my models generates the following migration:
|
Shadow FKs are not supported, so you should add FK property as well. It is stated in the ReadMe:
Related issue: BulkInsert does not populate foreign keys for child entities |
Support for Shadow FKs is now added, version 3.2.1+ |
Model
[Key] public int Id { get; set; } [Required] [Column("JobFunctionID")] public JobFunction JobFunction { get; set; } [Required] [Column("StatusID")] public Status Status { get; set; } [Required] [Column("EmployeeID")] public Employee Employee { get; set; } [Column(TypeName = "datetime")] public DateTime CreatedAt { get; set; } [Column(TypeName = "datetime")] public DateTime UpdatedAt { get; set; }
Insert
using (var transaction = _context.Database.BeginTransaction()) { if (entities.Count() > 0) { _context.BulkInsert(entities); } transaction.Commit(); }
Error
System.InvalidOperationException: The given value of type String from the data source cannot be converted to type int of the specified target column. ---> System.FormatException: Failed to convert parameter value from a String to a Int32. ---> System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type) at System.String.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at Microsoft.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming) --- End of inner exception stack trace --- at Microsoft.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming) at Microsoft.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed) --- End of inner exception stack trace --- at EFCore.BulkExtensions.SqlBulkOperation.Insert[T](DbContext context, Type type, IList
1 entities, TableInfo tableInfo, Action1 progress) at EFCore.BulkExtensions.SqlBulkOperation.Insert[T](DbContext context, IList
1 entities, TableInfo tableInfo, Action1 progress) at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, IList
1 entities, OperationType operationType, BulkConfig bulkConfig, Action1 progress) at EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsert[T](DbContext context, IList
1 entities, BulkConfig bulkConfig, Action1 progress) at TrainingManagement.Controllers.ManagementController.DataUpdateAsync(IFormFile file) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask
1 actionResultValueTask)at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)`
The text was updated successfully, but these errors were encountered: