UPDATED
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[bmaitempic](
[itemnmbr] [char](31) NOT NULL,
[fileseq] [int] NOT NULL,
[filename] [nvarchar](255) NULL,
[filedata] [varbinary](max) NULL,
[Compressed File Data] [varbinary](max) NULL
CONSTRAINT [PK_BMAITEMPIC] PRIMARY KEY CLUSTERED
(
[fileseq] ASC,
[itemnmbr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I have a simple model with a compound key:
[Table("bmaitempic")]
public class EcommerceItemImages
{
[Column("itemnmbr")]
[StringLength(31)]
public string Itemnmbr { get; set; }
[Column("fileseq")]
public int Fileseq { get; set; }
[Column("filename")]
[StringLength(255)]
public string Filename { get; set; }
[Column("filedata")]
public byte[] ImageData { get; set; }
[Column("Compressed File Data")]
public byte[] CompressedImageData { get; set; }
}
In my DbContext is like this:
public class EntityContext : DbContext
{
public EntityContext(DbContextOptions<EntityContext> options)
: base(options)
{
}
public DbSet<EcommerceItemImages> Images { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EcommerceItemImages>()
.HasKey(x => new { x.Itemnmbr, x.Fileseq });
}
}
I have a method in a Winforms button press event where I get an exception on SaveChanges():
private void btnConvert_Click(object sender, EventArgs e)
{
_context.Images.Load();
var images = _context.Images.Local.ToBindingList();
foreach (var image in images)
{
if(image.ImageData != null)
image.CompressedImageData = Compress(image.ImageData);
}
var count = _context.SaveChanges();
MessageBox.Show($@"{count} images compressed.");
}
The Exception is:
Microsoft.EntityFrameworkCore.DbUpdateException
HResult=0x80131500
Message=An error occurred while updating the entries. See the inner exception for details.
Source=Microsoft.EntityFrameworkCore.Relational
StackTrace:
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()
at CompressImages.Form1.btnConvert_Click(Object sender, EventArgs e) in D:\Projects\NewEcommerce\CompressImages\Form1.cs:line 43
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
SqlException: Conversion failed when converting the varchar value '1-A3483A ' to data type int.
'1-A3483A ' is the Itemnmbr
of the first record and is part of the compound key. Why is EntityFramework Core trying to Convert it to an Integer?
UPDATE
Log entry:
Failed executing DbCommand (56ms)
[Parameters=[@p1='16384', @p2='1-A3483A ' (Nullable = false) (Size = 31), @p0='0x1F8B080000000000000AAD5707501340974E202111A497808418A44A535A0202...' (Size = 8000)],
CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
UPDATE [bmaitempic] SET [Compressed File Data] = @p0 WHERE [fileseq] = @p1 AND [itemnmbr] = @p2;
SELECT @@ROWCOUNT;
For completeness:
Solution: I had a sql UPDATE Trigger on the table. I disabled it. Fix was provided by @LJRishe in a comment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.