MemoDb: Auto assign id support
MemoDb is a in-memory-linq-database useful for mocking linq repositories in unit testing. You can track the previous posts:
Today I will talk about the generation of ids for new entities. As MemoDb stands for mocking real linq orms and, generally, the underlying databases auto assign the ids.
var memo = new Memo()
.Map<MyEntity>(x=>x.Id(x => x.Id, autoAssign: true));
By now, MemoDb supports:
- Int32: increments the last id.
- Int64: increments the last id.
- Guid: Generates a new Guid.
If you need another strategy or to map another id type, you can specify the strategy as a delegate:
var memo = new Memo()
.Map<MyEntity>(x=>x.Id(x => x.Id, last => last + 10));
In the example above, the generation is made adding 10 to the last id assigned.
By default, MemoDb will assign the ids when the object is inserted. You can override this behavior by configuration:
var memo = new Memo(assignIdMode: AssignIdMode.OnFlush)
.Map<MyEntity>();
Advertisement
Categories: C#, MemoDb
c#, database, databases, Linq, software design, Unit testing
