I have a Merge stored proc. I mapped the proc to my entity for both insert and update. 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.
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. 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.
Then I changed the poc code to take the results of the collection and return the first 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.
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.