Skip to content
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

Invalid cast when calling SaveChanges() #19174

Closed
LordBenjamin opened this issue Dec 5, 2019 · 3 comments
Closed

Invalid cast when calling SaveChanges() #19174

LordBenjamin opened this issue Dec 5, 2019 · 3 comments

Comments

@LordBenjamin
Copy link

Called SaveChanges() after adding a simple entity.

Expected entity to be saved - actually got an exception.

Steps to reproduce

I haven't put together a minimum repro yet, but my method essentially looks like this:

var row = new User();
row.UserId = userId;
row.CreatedDateTimeUtc = DateTime.UtcNow;

context.Add(row);

row.Email = "test@example.com";
row.Name = "Test";
row.ExpiryDate = DateTime.UtcNow.AddDays(7);

context.SaveChanges();
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.

   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()

Inner exception:

Unable to cast object of type 'System.Data.SqlClient.SqlParameter' to type 'Microsoft.Data.SqlClient.SqlParameter'.

   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDateTimeTypeMapping.ConfigureParameter(DbParameter parameter)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping.CreateParameter(DbCommand command, String name, Object value, Nullable`1 nullable)
   at Microsoft.EntityFrameworkCore.Storage.Internal.TypeMappedRelationalParameter.AddDbParameter(DbCommand command, Object value)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)

Further technical details

EF Core version: 3.1.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Framework 4.7.2
Operating system: Windows 10 (1809)
IDE: Visual Studio 2019 16.3

@roji
Copy link
Member

roji commented Dec 5, 2019

Duplicate of #16812, see also the breaking change note.

tl;dr you need to reference Microsoft.Data.SqlClient instead of System.Data.SqlClient.

@LordBenjamin
Copy link
Author

Thanks for pointing that out, I was about to note that changing the column definition [ExpiryDate] [date] NULL to [ExpiryDate] [datetime] NULL works. That wouldn't have dealt with the root cause though.

I've updated to use Microsoft.Data.SqlClient throughout this application and that's fixed the issue.

Is it safe / possible to use Microsoft.Data.SqlClient everywhere that System.Data.SqlClient is currently used - e.g. I have a library that is shared between different applications using EF Core and LINQ-to-SQL, can I update the library to use Microsoft.Data.SqlClient and the old L2S applications will still be OK?

@roji
Copy link
Member

roji commented Dec 5, 2019

@LordBenjamin I'm not familiar with LINQ-to-SQL, although I'm guessing it may not be possible for it to work with Microsoft.Data.SqlClient. However, it should be OK to have the same application with EF Core using M.D.SqlClient, and L2S using S.D.SqlClient. As a general rule M.D.SqlClient is very compatible with S.D.SqlClient, and since it's more actively developed, it will contain more fixes and features as time goes on.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants