gpt4 book ai didi

c# - Entity Framework - 无法更新 EntitySet 二进制数据

转载 作者:太空宇宙 更新时间:2023-11-03 14:23:52 39 4
gpt4 key购买 nike

Entity Framework 抛出错误:

Test 'WorkerProcessService.Test.WorkerProcessMonitorTests.Test' failed: System.Data.UpdateException : Unable to update the EntitySet 'Processor' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
at System.Data.SqlClient.SqlGen.DmlSqlGenerator.ExpressionTranslator.Visit(DbScanExpression expression)
at System.Data.Common.CommandTrees.DbScanExpression.Accept(DbExpressionVisitor visitor)
at System.Data.SqlClient.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, SqlVersion sqlVersion, List`1& parameters)
at System.Data.SqlClient.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, SqlVersion sqlVersion, List`1& parameters, CommandType& commandType, HashSet`1& paramsToForceNonUnicode)
at System.Data.SqlClient.SqlProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
at System.Data.SqlClient.SqlProviderServices.CreateCommand(DbCommandTree commandTree)
at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
WorkerProcessMonitor.cs(79,0): at Star.Portal.Services.WorkerProcessMonitor.AddComponent(Byte[] component)
WorkerProcessMonitorTests.cs(55,0): at WorkerProcessService.Test.WorkerProcessMonitorTests.Test()

我有一个类似的表格和自动生成的 edmx 模型

CREATE TABLE [dbo].[Processor](
[ProcessorID] [int] IDENTITY(1,1) NOT NULL,
[ProcessorDLL] [varbinary](max) NULL,
)

以下更新失败

public void AddComponent(byte[] component)
{
Processor p = new Processor()
{
ProcessorDLL = component,
};
using (var cn = GetWorkerProcessEntities())
{

cn.AddToProcessors(p);
cn.SaveChanges();
}

}

问题是:我是否必须做任何特定的事情才能在 sql server 中实现二进制存储?

最佳答案

简单的回答...不,我将图像存储在具有相同数据类型 (varbinary(max)) 的表中。

我不太了解您的代码,我使用本地数据库进行了简单测试。这是我的 POCO 和 DBcontext。 (我不使用自动生成的 edmx)

using System.Data.Entity.ModelConfiguration;
public class Processor
{
public int ProcessorId { get; set; }
public byte[] ProcessorDLL { get; set; }
}
public class ProcessorConfiguration : EntityTypeConfiguration<Processor>
{
public ProcessorConfiguration()
{
HasKey(i => i.ProcessorId);

ToTable("Processor", "dbo");
}
}
//DbContext
public class myContext : DbContext
{
public DbSet<Processor> Processors { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ProcessorConfiguration());
base.OnModelCreating(modelBuilder);
}
}

然后当我添加一个新的处理器时,我使用这个

public void test()
{
myContext context = new myContext();

Processor p = new Processor
{
ProcessorDLL = getSomeRandomByteArray()
};

context.Processors.Add(p);
context.SaveChanges();
}

在我的 table 上:

ProcessorID: 1
ProcessorDLL: 0xFFD8FFE000104A46494600010200...

希望这对您有所帮助。

关于c# - Entity Framework - 无法更新 EntitySet 二进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4626753/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com