How To Insert - SqlRepo/SqlRepo GitHub Wiki
This guide shows how you can insert rows into a database from instances of entities. It assumes you have followed one of the Getting Started guides Getting Started (IoC) or Getting Started (Static) and have created a repository.
Explicit Insert
We recommend you use this explicit insert method as it gives you more control of the SQL generated and what values actually get inserted. We provide an alternative implicit insert method, which is detailed below but as you will see it has potential issues. With the explicit method you have full control of which columns are given values allowing things like default constraints to be applied.
var result = repository.Insert()
.With(e => e.Task, "Book theatre")
.Go();
Here we have provided the only required value using the With() method. The defaults for CreatedDate and IsCompleted will be applied by SqlServer.
The With() method takes an expression for the member to be set and a value, it can be called for each member that should have a value set.
Implicit Insert
The simplest way to insert a database row for an entity instance is to pass the instance to the repository Insert method. However SqlRepo has no knowledge or how the entity maps to the relevant table so it will generate a statement that inserts a value for every writable property of the entity, even if that property value is null. This could be in conflict with things like defaults and constraints defined on the database table.
repository.Insert(myEntity);
Note you don't have to call Go() in this case the statement is generated and executed immediately. This is shorthand for the following, which is what is actually executed.
repository.Insert().For(myEntity).Go();