henkmollema / Dommel

CRUD operations with Dapper made simple.
MIT License
611 stars 99 forks source link

DatabaseGeneratedOption.None not working #304

Closed denovellis closed 1 month ago

denovellis commented 5 months ago

Hi,

I've encountered a problem where, even though DatabaseGeneratedOption is set to None, the system is still attempting to perform an insertion with an auto-increment value instead of using the provided value for the insert.

Steps to Reproduce: Configure a model with DatabaseGeneratedOption set to None. Attempt to insert a record into the database by explicitly providing a value for the primary key. Observe that an auto-increment value is being used instead.

Expected Behavior: The system should use the value provided for the insertion, as DatabaseGeneratedOption.None suggests that the value will not be automatically generated by the database.

Generated sql (found via profiler):

exec sp_executesql N'set nocount on insert into [Business] ([Name], [Count]) values (@test_name, @test_count); select scope_identity()',N'@test_count int,@test_name nvarchar(4000)',@test_count=0,@test_name=N'test'

` class Program { static void Main(string[] args) { Dapper.FluentMap.FluentMapper.Initialize(config => { config.AddMap(new TestMap()); config.ForDommel(); });

      var conn = new SqlConnection("XXXXXX");

      var a1 = conn.SelectAsync<Test>(test => test.test_name.Contains("*test*")).Result;

      Console.WriteLine(a1.Dump());

      var a2 = conn.InsertAsync<Test>(new Test { test_id = "X", test_name = "test", test_count = 0 }).Result;
      Console.WriteLine(a2);
  }

}

public class Test { public string test_id { get; set; } public string test_name { get; set; } public int test_count { get; set; }

}

public class TestMap : DommelEntityMap { public TestMap() { ToTable("Business");

      Map(p => p.test_id).ToColumn("Id", caseSensitive: true).IsKey()
          .SetGeneratedOption(DatabaseGeneratedOption.None);

      Map(p => p.test_name).ToColumn("Name", caseSensitive: true);
      Map(p => p.test_count).ToColumn("Count", caseSensitive: true);
  }

}`

denovellis commented 5 months ago

I would like to provide some additional information regarding the issue I reported earlier with DatabaseGeneratedOption.None. I've noticed an interesting behavior that might help in diagnosing the problem:

When I use the source code directly from the repositories of Dommel and FluentMap.Dommel, everything works as expected, and the system correctly uses the provided value for insertion without attempting to auto-increment.

However, when I switch to using the NuGet packages of these libraries, even though the versions are identical to those in the source code I initially tested with, the problem occurs where it tries to insert an auto-increment value instead.

This behavior suggests that there might be a discrepancy between the packaged versions on NuGet and the latest source code in the repositories, despite them having identical version numbers.

henkmollema commented 2 months ago

Dapper.FluentMap is archived and might not be compatible with the latest version of Dommel. Can you try using attribute mapping instead? E.g. [DatabaseGenerated(DatabaseGeneratedOption.None)].