forked from schotime/NPoco
-
Notifications
You must be signed in to change notification settings - Fork 0
Create Read Update Delete
schotime edited this page May 15, 2012
·
4 revisions
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);