How can I ignore a property on my model using dapper/dapper extensions/dapper rainbow or any
of those dapper libraries?
How can I ignore a property on my model using dapper/dapper extensions/dapper rainbow or any
of those dapper libraries?
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();
}
}
}
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.
[Write(false)]
attribute. Can anyone tell what is the difference between [Computed]
and [Write(false)]
? –
Aborticide Computed
attribute and wasn't aware of any Write
attribute at the time. –
Angstrom [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 [Write(false)]
and [Computed]
, refer to this question. –
Flambeau 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();
}
}
}
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; }
}
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);
For those not wanting to include DapperExtensions, DatabaseGenerated
from the standard System.ComponentModel.DataAnnotations.Schema
can be used also.
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
© 2022 - 2024 — McMap. All rights reserved.