[英]Why is Entity Framework trying to store my foreign key for a second time?
[英]Why is Entity Framework Core trying to Convert my string key to an Integer?
更新
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
我有一个带有复合键的简单模型:
[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; }
}
在我的 DbContext 中是这样的:
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 });
}
}
我在 Winforms 按钮按下事件中有一个方法,我在 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.");
}
例外是:
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 ' 是第一条记录的Itemnmbr
并且是复合键的一部分。 为什么 EntityFramework Core 试图将其转换为整数?
更新
日志条目:
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;
为了完整性:
解决方案:我在表上有一个 sql UPDATE Trigger。 我禁用了它。 @LJRishe 在评论中提供了修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.