Skip to content

How to use Merge #1291

Description

@bpaolo71

Hello,

I'm not sure how to use the Merge function. This is my test case:
RepoDb 1.15.1, RepoDb.MySqlConnector 1.15.0, Database MySql

CREATE TABLE test_merge (
Id int unsigned NOT NULL AUTO_INCREMENT,
Prop1 varchar(45) COLLATE utf8mb4_0900_as_ci NOT NULL,
Prop2 varchar(45) COLLATE utf8mb4_0900_as_ci NOT NULL DEFAULT '',
PRIMARY KEY (Id),
UNIQUE KEY UNIQUE (Prop1,Prop2)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_ci;

internal class TestMerge
{
public uint Id { get; set; }
public string Prop1 { get; set; } = string.Empty;
public string Prop2 { get; set; } = string.Empty;
}

internal class TestMergeRepository : DbRepository
{
static TestMergeRepository()
{
FluentMapper
.Entity()
.Table("test_merge");
}

public TestMergeRepository(DatabaseContext dbContext) : base(dbContext.ConnectionString, new RepositoryTrace(write: message => Console.WriteLine(message))) { }

}

[TestMethod]
public void Test_Merge()
{
var entity1 = new TestMerge()
{
Prop1 = "Value1",
Prop2 = "Value2"
};
var result1 = repository.Merge(
entity1,
fields: Field.Parse(a => new { a.Prop1, a.Prop2 }),
qualifiers: (a => new
{
a.Prop1,
a.Prop2
})
);
Console.WriteLine(result1);

var entity2 = new TestMerge()
{
    Prop1 = "Value1",
    Prop2 = "Value2"
};
var result2 = repository.Merge(
    entity2,
    fields: Field.Parse<TestMerge>(a => new { a.Prop1, a.Prop2 }),
    qualifiers: (a => new
    {
        a.Prop1,
        a.Prop2
    })
);
Console.WriteLine(result2);

}

I get this error:
Test method DomainLib.Data.MySql.Tests.Repositories.MergeTests.Test_Merge threw exception:
MySqlConnector.MySqlException: Parameter '@id' must be defined. To use this as a variable, set 'Allow User Variables=true' in the connection string.

The query: INSERT INTO test_merge ( Prop1, Prop2 ) VALUES ( @Prop1, @prop2 ) ON DUPLICATE KEY UPDATE Prop1 = @Prop1, Prop2 = @prop2 ; SELECT COALESCE(@id, LAST_INSERT_ID()) AS Result ;

Metadata

Metadata

Assignees

Labels

deployedFeature or bug is deployed at the current releasequestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions