user2412672
user2412672

Reputation: 1479

Dapper ignore property runtime

Is there a way to ignore property from being mapped in runtime. Because I don't know if database has specific column and I have to check it before doing insert. If database doesn't have column then I want to ignore this one specific property.

UPDATE:

Here's my insert code

    public static void Insert(string connectionString, T entity)
    {
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();

            connection.Insert(entity);
        }
    }

Upvotes: 1

Views: 4780

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131364

That Insert method is part of Dapper.Contrib, not Dapper itself. As the Readme for that library explains, you can use the [Write(false)] attribute to specify that a property isn't writeable, eg :

public class MyClass
{
    public int ID {get;set;}

    public DateTime Created{get;set;}

    [Write(false)]
    public DateTime CreatedDate =>Created.Date;

}

The source code shows that Dapper.Contrib simply ignores properties that aren't writable :

var properties = type.GetProperties().Where(IsWriteable).ToArray();

Dapper is a microORM, it doesn't offer the mapping features found in full ORMs like EF or NHibernate. Dapper.Contrib adds some helper methods and very basic mapping through 5 atrributes:

  • [Table("Tablename")] to specify the table name
  • [Key] to mark an auto-generated key field
  • [ExplicitKey] to mark a field that isn't generated automatically
  • [Write(true/false)] to mark (non)writable properties
  • [Computed] to mark calculated properties.

There's no way to specify a column name for example

Upvotes: 2

Related Questions