Ignore property on model property [closed]
Asked Answered
I

5

29

How can I ignore a property on my model using dapper/dapper extensions/dapper rainbow or any

of those dapper libraries?

Ibnsina answered 31/10, 2013 at 21:4 Comment(3)
For Insert<Person>(person). person has kind of computed property. Which does not belong to the database.Ibnsina
Since non of the answers below provide a solution for a Dapper.Rainbow based project, I am adding this comment. Dapper Rainbow has an IgnoreProperty Attribute that can be used. Your POCO class has to reference Dapper.Rainbow.{SQL} and then you can use [IgnoreProperty(true)] for properties that you wish to excludePederasty
Related for Dapper.Contrib: https://mcmap.net/q/501476/-what-39-s-the-difference-between-computed-and-write-false-attributes/5779732Flambeau
U
17

Dapper creator Sam Saffron has addressed this requirement in response to another SO user's questions here. Check it out.

Also, if you want to use the Dapper Extensions library that Sam has mentioned in his answer, you can get it from Github or via Nuget.

Here's an example of ignoring properties from the Library's Test Project.

using System;
using System.Collections.Generic;
using DapperExtensions.Mapper;

namespace DapperExtensions.Test.Data
{
    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DateCreated { get; set; }
        public bool Active { get; set; }
        public IEnumerable<Phone> Phones { get; private set; }
    }

    public class Phone
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

    public class PersonMapper : ClassMapper<Person>
    {
        public PersonMapper()
        {
            Table("Person");
            Map(m => m.Phones).Ignore();
            AutoMap();
        }
    }
}
Uphroe answered 2/11, 2013 at 0:58 Comment(3)
Where/When do you instantiate the PersonMapper class ? Is dapper extensions looking for Mapper class by taking the entityname + "Mapper" string and trying to instantiate it?Ibnsina
Yes, I believe the AutoMapper feature of Dapper Extensions looks for singular entity name + "Mapper" suffix to automap fields to ignore, fields with different names etc. In other words, I think (haven't tried it) you should be ok with just defining this PersonMapper class and ignoring the fields you want to ignore from the POCO. See the documentation here: github.com/tmsmith/Dapper-Extensions/wiki/AutoClassMapperUphroe
Fine it worked for me. My XXXMapper class has the same namespace.Ibnsina
A
31

Dapper.Contrib has built-in support for marking a column as computed: add ComputedAttribute to allow support for computed columns on Insert. Here's how it works:

class MyModel
{
  public string Property1 { get; set; }

  [Computed]
  public int ComputedProperty { get; set; }
}

Properties marked with the Computed attribute will be ignored on inserts.

Angstrom answered 5/11, 2014 at 20:19 Comment(4)
There is also [Write(false)] attribute. Can anyone tell what is the difference between [Computed] and [Write(false)]?Aborticide
@Aborticide my best guess is that they were added at different times by different people who may not have been aware of the other. I added the Computed attribute and wasn't aware of any Write attribute at the time.Angstrom
They mean something different in my opinion - [Computed] means "this column is in the database but is a Computed column" but [Write(false)] means "this column is not in the database"Reuven
For details about [Write(false)] and [Computed], refer to this question.Flambeau
U
17

Dapper creator Sam Saffron has addressed this requirement in response to another SO user's questions here. Check it out.

Also, if you want to use the Dapper Extensions library that Sam has mentioned in his answer, you can get it from Github or via Nuget.

Here's an example of ignoring properties from the Library's Test Project.

using System;
using System.Collections.Generic;
using DapperExtensions.Mapper;

namespace DapperExtensions.Test.Data
{
    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DateCreated { get; set; }
        public bool Active { get; set; }
        public IEnumerable<Phone> Phones { get; private set; }
    }

    public class Phone
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

    public class PersonMapper : ClassMapper<Person>
    {
        public PersonMapper()
        {
            Table("Person");
            Map(m => m.Phones).Ignore();
            AutoMap();
        }
    }
}
Uphroe answered 2/11, 2013 at 0:58 Comment(3)
Where/When do you instantiate the PersonMapper class ? Is dapper extensions looking for Mapper class by taking the entityname + "Mapper" string and trying to instantiate it?Ibnsina
Yes, I believe the AutoMapper feature of Dapper Extensions looks for singular entity name + "Mapper" suffix to automap fields to ignore, fields with different names etc. In other words, I think (haven't tried it) you should be ok with just defining this PersonMapper class and ignoring the fields you want to ignore from the POCO. See the documentation here: github.com/tmsmith/Dapper-Extensions/wiki/AutoClassMapperUphroe
Fine it worked for me. My XXXMapper class has the same namespace.Ibnsina
T
11

In my case I used Dapper.Contrib.
Using [Write(false)] attribute on any property should solve the problem. Some also suggest using [Computed] attribute.

public class Person
{        
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }

    [Write(false)]
    public IEnumerable<Email> Emails { get; }
}
Tacheometer answered 8/5, 2017 at 15:58 Comment(0)
G
5

You can design a base class without the computed property and use that for your inserts.

  class BasePerson
    {
      public String Name {get;set;}
    }

    class Person: BasePerson
    {
     public String ComputedProperty {get;set;}
    }

    Insert<BasePerson>(person);
Gonzales answered 1/11, 2013 at 13:30 Comment(0)
T
5

For those not wanting to include DapperExtensions, DatabaseGenerated from the standard System.ComponentModel.DataAnnotations.Schema can be used also.

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
Tosch answered 19/7, 2017 at 15:4 Comment(3)
This solution wont works with Dapper 1.50.2, on .NET COREOlvera
This didn't work for Insert/Update in Dapper.ContribXavierxaviera
Using standard annotations as opposed to custom attributes should be the preferred choice for Dapper.ContribBriton

© 2022 - 2024 — McMap. All rights reserved.