简体   繁体   English

如何使用Dapper.net Extensions忽略类属性?

[英]How to ignore a class property using Dapper.net Extensions?

I am using Dapper.net Extensions and would like to ignore certain properties without having to write a complete custom mapper. 我正在使用Dapper.net Extensions,并且想要忽略某些属性而无需编写完整的自定义映射器。 As you can see in the ClassMapper below there is a lot of redundant code when all I really want to do is ignore one property. 正如您在下面的ClassMapper中看到的那样,当我真正想做的就是忽略一个属性时,会有很多冗余代码。 What would be the best way to accomplish this? 实现这一目标的最佳方法是什么?

I like the answer provided here https://stackoverflow.com/a/14649356 but I cannot find the namespace where 'Write' is defined. 我喜欢这里提供的答案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();
    }
}

The WriteAttribute class is located in the Dapper.Contrib.Extensions namespace--which is part of the Dapper.Contrib project. WriteAttribute类位于Dapper.Contrib.Extensions命名空间中 - 它是Dapper.Contrib项目的一部分。 You can add that via nuget, the package is named "Dapper.Contrib" 您可以通过nuget添加该包,该包名为“Dapper.Contrib”

As you can see in Person.cs , just call AutoMap(); 正如您在Person.cs中看到的那样,只需调用AutoMap(); in constructor of you ClassMapper . 在你的构造函数ClassMapper For example: 例如:

public class CustomMapper : DapperExtensions.Mapper.ClassMapper<Photo>
{
    public CustomMapper()
    {
        Map(x => x.PhotoId).Key(KeyType.Identity);
        Map(f => f.SomePropertyIDontCareAbout).Ignore();
        AutoMap();
    }
}

You can decorate the property with [Computed] and the property will be ignored on insert. 您可以使用[Computed]修饰属性,并且在插入时将忽略该属性。 Semantically it may not be perfect but it seems to do the job: 在语义上它可能不完美,但它似乎做的工作:

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

Then again, Peter Ritchie's answer is likely more spot-on with: 再说一遍,Peter Ritchie的回答可能更多的是:

[WriteAttribute(false)]
public int SomePropertyIDontCareAbout { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM