Inserting and updating data
A quick note
You will notice that there are multiple ways to achieve the same thing here. Which you choose to use are up to you, but I recommend that you try and be consistent within your project. My preferred approach is to ensure that my referential integrity is properly configured, and then let Simple.Data's discovery handle more stuff for me.
Inserting
Insert (with Named Arguments)
Simple.Data:
db.Users.Insert(Name: "steve", Age: 50)
SQL:
INSERT INTO Users (Name, Age) values (@p0, @p1)
Insert (using an object)
**Simple.Data: **
dynamic person = new ExpandoObject(); person.Name = "steve"; person.Age = 50; db.Users.Insert(person);
SQL:
INSERT INTO Users values (@p0,@p1)
If you have an IDENTITY column defined on your table, then the Insert methods will all return a copy of the record fetched from the database, so all defaulted values are set. In 0.4, support for other ways of fetching the inserted row will be added.
Updating
UpdateBy (using Named Arguments)
Simple.Data:
db.Users.UpdateById(Id: 1, Name: "Dave", Age: 49)
SQL:
UPDATE [Users] SET [Name] = @p1, [Age] = @p2 WHERE [Users].[Id] = @p3
Update (using Dynamic or Static Object)
If the table has a Primary Key defined, you can just call Update and pass in a dynamic or statically-typed data object:
Simple.Data:
var user = db.Users.FindById(1);
user.Name = "Dave";
user.Age = 49;
db.Users.Update(user);
SQL:
UPDATE [Users] SET [Name] = @p1, [Age] = @p2 WHERE [Users].[Id] = @p3
If there is no Primary Key defined on the table, you can use UpdateBy to tell Simple.Data which columns to use as criteria:
Simple.Data:
User user = db.Users.FindById(1);
user.Name = "Dave";
user.Age = 49;
db.Users.UpdateById(user);
SQL:
UPDATE [Users] SET [Name] = @p1, [Age] = @p2 WHERE [Users].[Id] = @p3