Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以将常量绑定到实体框架插入/更新函数映射吗?_C#_Sql Server_Stored Procedures_Entity Framework 4 - Fatal编程技术网

C# 我可以将常量绑定到实体框架插入/更新函数映射吗?

C# 我可以将常量绑定到实体框架插入/更新函数映射吗?,c#,sql-server,stored-procedures,entity-framework-4,C#,Sql Server,Stored Procedures,Entity Framework 4,我们正在升级一个遗留系统,该系统有一个自定义ORM来使用EF4.3 遗留系统使用混合“保存”存储过程,其中proc本身根据主键的存在确定是否插入或更新实体,具体如下: -- Hybrid Insert if not exist / Update if does exist CREATE PROC dbo.SaveEntity @EntityId INT = NULL, -- This field is specified on an update,

我们正在升级一个遗留系统,该系统有一个自定义ORM来使用EF4.3

遗留系统使用混合“保存”存储过程,其中proc本身根据主键的存在确定是否插入或更新实体,具体如下:

-- Hybrid Insert if not exist / Update if does exist
CREATE PROC dbo.SaveEntity
        @EntityId INT = NULL, -- This field is specified on an update, 
                              -- and left to its default on inserts
        @Field1 NVARCHAR(50)
        -- @Field2 etc etc
AS
    BEGIN
        -- IF Exists (Entity with PK @EntityId) then insert ...
        --   BEGIN
        --     INSERT INTO Entity(Field1, ...) VALUES (@Field1, ...)
        --     SELECT @EntityId = SCOPE_IDENTITY()    
        --   END
        -- ELSE
        --   BEGIN
        --     UPDATE Entity SET Field1 = @Field1, ... WHERE EntityId = @EntityId
        --   END

        -- Return the PK Here
        SELECT @EntityId AS EntityId
    END
在EF中,我将“save”存储过程添加到模型中,并将Insert和Update函数映射到各个实体的映射详细信息中的“save”过程中

Update函数工作正常-因为返回结果不会更改,所以不需要将其映射回实体

然而,插入映射使我陷入了一个两难境地。对于Insert函数,我需要将存储过程返回的指定PK标识值映射回实体PK。但是,由于Proc(具有可选的@EntityId参数)的性质,EF还要求映射所有参数,即使@EntityId是可选的和默认的

如果无法将EntityId映射到PROC字段,则会出现编译错误:

A mapping function bindings specifies a function Model.Store.SaveEntity 
but does not map the following function parameters: EntityId
如果我将EntityId绑定到proc字段,并作为列绑定的结果,我会得到运行时错误(因为字段只能是输入或输出映射中的一个,但是):

所以我的问题是,我如何摆脱这种情况?。e、 g.我是否有办法将常量(即零或空)绑定到我的Insert Proc字段映射


请注意:其中一些过程中包含额外的逻辑(内务管理、审计等),因此我不愿意完全抛弃它们,或者将过程重写为单独的插入/更新过程,因为它们已经得到了很好的验证,更改基础DB DDL不是我们升级任务的一部分。

您可以随时编写更新(或插入)存储过程,作为插入/更新存储过程的一个非常薄的包装。@GertArnold谢谢-说得好。我将尝试一个插入包装器,它只返回PK并让您知道。但我不敢相信没有办法让EF忽略/默认映射?
Unable to determine a valid ordering for dependent operations. 
Dependencies may exist due to foreign key constraints, model requirements, 
or store-generated values.