Home

Bart Read edited this page Dec 11, 2015 · 29 revisions

Dapper.SimpleSave simplifies the process of saving complex object hierarchies to a relational database.

If instead (or in addition) you need to load complex object hierarchies from a relational database, check out Dapper.SimpleLoad, which obviates the need to write complex and hard to maintain Dapper multi-mapping code.

##Example

    [Table("[user].USER_MST")]
    public class UserDao
    {
        [PrimaryKey]
        public int? UserKey { get; set; }
        public Guid UserGUID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string PasswordSalt { get; set; }
        [Column("UserStatusKey")]
        [ManyToOne]
        public UserStatusEnum Status { get; set; }
        public string CountryCode { get; set; }
        public string PersonalMobileNumber { get; set; }
        public string BusinessMobileNumber { get; set; }
        [Column("OfficeNumberKey")]
        [OneToOne]
        [ForeignKeyReference(typeof(OfficeNumberDao))]
        public OfficeNumberDao OfficeNumber { get; set; }
        public string VirtualLineNumber { get; set; }
        public string EmailAddress { get; set; }
        public int PasswordFailureCount { get; set; }
        [ManyToOne]
        [Column("PositionKey")]
        public PositionDao Position { get; set; }
        [ManyToMany("[user].USER_DEPARTMENT_LNK")]
        public IList<DepartmentDao> Department { get; set; }
        [ManyToMany("[user].USER_ADDITIONAL_ROLES_LNK")]
        public IList<RoleDao> AdditionalRoles { get; set; }
        [ManyToMany("[user].USER_TEAM_LNK")]
        public IList<TeamDao> Team { get; set; }
    }

    ...

    var user = new UserDao {
        FirstName = "Marty",
        LastName = "McFly",
        Username = "marty.mcfly",
        Department = new List<DepartmentDao> {
            new DepartmentDao {
                Name = "Time Travel"
            }
        }
        ....
    }

    using (var connection = new SqlConnection(connectionString))
    {
        // INSERTs rows for UserDao, DepartmentDao, and other child objects
        // in the relevant tables.
        connection.Create(user);
    }

##Documentation

  1. Getting Started
  2. How can Dapper.SimpleSave help me?
  3. Tutorial: Saving data to a database
  4. Configuring the behaviour of Dapper.SimpleSave with attributes
  5. Transactions
  6. Extension method reference
  7. Dapper.SimpleSave and cardinality - 1:1, 1:N, N:1, and M:N relationships
  8. How does Dapper.SimpleSave work?
  9. Known and intended limitations
  10. FAQ