-
Notifications
You must be signed in to change notification settings - Fork 302
Create Read Update Delete
This wiki page refers to the User
class found here.
IDatabase db = new Database("connStringName");
User u = new User()
{
Email = "name@domain.com",
LastLoggedIn = DateTime.UtcNow
};
db.Insert(u);
This will insert the User
object into the users table generating a new identity value. This value will be populated back into the object after the insert statement. For example, the following would be true.
var user = db.SingleById(u.UserId);
Assert.AreEqual(u.Email, user.Email);
Once I have the object, I can update its properties. After calling the Update
method, those changes will be persisted to the database.
var user = db.SingleById(1);
user.Email = "new@domain.com";
db.Update(user);
If I decide I no longer need the record I can delete it in a very similar fashion to Insert
and Update
. That is by passing the object to the Delete
method. Just the primary key value can also be passed to the Delete
method, however the generic type parameter will need to be specified.
var user = db.SingleById(1);
db.Delete(user);
or
db.Delete<User>(1);
This will insert a record if it is new or update an existing record if it already exists. Its existence is determined by its primary key.
IDatabase db = new Database("connStringName");
User u = new User()
{
Email = "name@domain.com",
LastLoggedIn = DateTime.UtcNow
};
db.Save(u);