简体   繁体   English

合并到语句和实体框架中

[英]Merge into statement and Entity Framework

I have a Merge stored proc.我有一个合并存储过程。 I mapped the proc to my entity for both insert and update.我将 proc 映射到我的实体以进行插入和更新。 When I run I get the following error - Any Ideas?当我运行时,我收到以下错误 - 有什么想法吗?

Schema specified is not valid.指定的架构无效。 Errors: Model1.msl(23,14): error 2038: The parameter DataField is bound multiple times.错误:Model1.msl(23,14):错误 2038:参数 DataField 被多次绑定。

The Proc:过程:

ALTER PROCEDURE [dbo].[USP_UPSERT_SimpleTableExample]
(
    @NaturalKey1 nchar(10),
    @NaturalKey2 nchar(10),
    @NaturalKey3 nchar(10),
    @DataField nchar(10)
 )

AS
BEGIN
    -- Start Transaction
    BEGIN TRAN

    MERGE INTO dbo.SimpleTableExample ChangeSet
    USING (SELECT   @NaturalKey1 as key1,
                    @NaturalKey2 as key2,
                    @NaturalKey3 as key3) CurrentSet
    ON  ChangeSet.NaturalKey1 = CurrentSet.key1 AND
        ChangeSet.NaturalKey2 = CurrentSet.key2 AND
        ChangeSet.NaturalKey3 = CurrentSet.key3     
    WHEN MATCHED THEN 
        UPDATE SET DataField = @DataField

    WHEN NOT MATCHED 
        THEN INSERT VALUES
           (@NaturalKey1,
           @NaturalKey2,
           @NaturalKey3,
           @DataField) 

    OUTPUT INSERTED.SurrogateKey;

    COMMIT TRAN        

END

And My test code;还有我的测试代码;

static void Main(string[] args)
{
    Class1 c1 = new Class1();

    var test = new SimpleTableExample();
    test.DataField = "data1";
    test.NaturalKey1 = "1";
    test.NaturalKey2 = "2";
    test.NaturalKey3 = "3";

    c1.test(test);
}

public string test(SimpleTableExample ste)
{

    ExamplesEntities1 ex1 = new ExamplesEntities1();

    ex1.AddToSimpleTableExamples(ste);
    ex1.SaveChanges();

    Console.WriteLine("SurrogateKey:0", ste.SurrogateKey);
    Console.WriteLine("EntityKey:0", ste.EntityKey);
    return ste.EntityKey.ToString();
}

Problem solved.问题解决了。 The error was added because I mapped the Upsert proc to the table two times, once as the insert and once as the update.添加错误是因为我将 Upsert proc 映射到表两次,一次作为插入,一次作为更新。 This was wrong!这是错误的!

I removed the table mapping to the stored proc.我删除了到存储过程的表映射。

I changed the stored proc to instead of returning the surogate key to return the entire dataset of what was inserted or updated, which includes the surrogate key.我将存储过程更改为而不是返回代理键来返回插入或更新的整个数据集,其中包括代理键。

Next I added function import to the storedproc and had it return a collection so my table.接下来,我将 function 导入添加到存储过程并让它返回一个集合,所以我的表。

Then I changed the poc code to take the results of the collection and return the first surrogatekey;然后我改了poc代码,取集合的结果,返回第一个surrogatekey;

public string test(SimpleTableExample ste)
        {

            ExamplesEntities1 ex1 = new ExamplesEntities1();
            var results = ex1.USP_UPSERT_SimpleTableExample(ste.NaturalKey1, ste.NaturalKey2, ste.NaturalKey3, ste.DataField).ToList<SimpleTableExample>();
            string returnvalue = results.First().SurrogateKey.ToString();
            return returnvalue;
        }

A Test did an insert when there was no matching naturalkeys and an update when natural keys matched.测试在没有匹配的自然键时进行插入,并在自然键匹配时进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM