[英]How to ignore a class property using Dapper.net Extensions?
我正在使用Dapper.net Extensions,並且想要忽略某些屬性而無需編寫完整的自定義映射器。 正如您在下面的ClassMapper中看到的那樣,當我真正想做的就是忽略一個屬性時,會有很多冗余代碼。 實現這一目標的最佳方法是什么?
我喜歡這里提供的答案https://stackoverflow.com/a/14649356但我找不到定義'Write'的命名空間。
public class Photo : CRUD, EntityElement
{
public Int32 PhotoId { get; set; }
public Guid ObjectKey { get; set; }
public Int16 Width { get; set; }
public Int16 Height { get; set; }
public EntityObjectStatus ObjectStatus { get; set; }
public PhotoObjectType PhotoType { get; set; }
public PhotoFormat2 ImageFormat { get; set; }
public Int32 CategoryId { get; set; }
public int SomePropertyIDontCareAbout { get; set; }
}
public class CustomMapper : DapperExtensions.Mapper.ClassMapper<Photo>
{
public CustomMapper()
{
Map(x => x.PhotoId).Column("PhotoId").Key(KeyType.Identity);
Map(x => x.ObjectKey).Column("ObjectKey");
Map(x => x.Width).Column("Width");
Map(x => x.Height).Column("Height");
Map(x => x.ObjectStatus).Column("ObjectStatus");
Map(x => x.PhotoType).Column("PhotoType");
Map(x => x.ImageFormat).Column("ImageFormat");
Map(x => x.CategoryId).Column("CategoryId");
Map(f => f.SomePropertyIDontCareAbout).Ignore();
}
}
WriteAttribute
類位於Dapper.Contrib.Extensions
命名空間中 - 它是Dapper.Contrib項目的一部分。 您可以通過nuget添加該包,該包名為“Dapper.Contrib”
正如您在Person.cs中看到的那樣,只需調用AutoMap();
在你的構造函數ClassMapper
。 例如:
public class CustomMapper : DapperExtensions.Mapper.ClassMapper<Photo>
{
public CustomMapper()
{
Map(x => x.PhotoId).Key(KeyType.Identity);
Map(f => f.SomePropertyIDontCareAbout).Ignore();
AutoMap();
}
}
您可以使用[Computed]修飾屬性,並且在插入時將忽略該屬性。 在語義上它可能不完美,但它似乎做的工作:
[Computed]
public int SomePropertyIDontCareAbout { get; set; }
再說一遍,Peter Ritchie的回答可能更多的是:
[WriteAttribute(false)]
public int SomePropertyIDontCareAbout { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.