asp.netvb.netinsertpetapocomicro-orm

PetaPoco \ NPoco - Partial Insert


Is there a way to partially insert an object using PetaPoco or NPoco? For example, I have a table called Users:

UserId | UserName | UserMail | UserCreationDate

Each one of these columns are NON NULLABLE and have a default value when they are left empty.

In ASP.NET I have a User class, and I use the ORM to insert a new record with only the name:

Dim userData As New User()
userData.UserName = "Jimmy Hendrix"
db.Insert(userData)

I expect the database to look as follows:

UserId |   UserName    |   UserMail  | UserCreationDate
  12   | Jimmy Hendrix | (DB default)|  (DB default)

I want the insert command only insert the name, without inserting the other object properties with the object's default values.

Such as there is a partial update, I want a partial insert. Is that possible in PetaPoco?

Is there another way to do it by myself without any ORM?

Edit:

Using SQL I can get the job done, but I need to use POCO objects, so I don't want to have to remember the database parameters. I want something like

user.UserName = "Michael"
user.Insert(user)

And it will insert only the UserName, ignoring the other variables. The SQL that I want to be generated in the background is:

"INSERT Users(UserName) VALUES(@UserName)"
(while the @UserName parameter holds the userData.FirstName value)

As you can see, it doesn't take in account the other variables in the class. Today if I use the insert command, even if I give a value to a single property in the class, NPoco still tries to insert ALL the class variables into the db setting the variables I didn't want to set with the class's default values (which are different from the db default values)

Also, all of the properties are insertable/updateable, so there can't be any ResultColumn types in the class. I want to insert these values but only the ones I declare in that particular instance. All of the properties are available to update and insert but for each instance i insert only what i declare.


Solution

  • I would create a PartialUserForInsert class:

    [TableName("Users")]
    public class PartialUserForInsert
    {
        public string UserName { get; set; }
    }