DapperAOT icon indicating copy to clipboard operation
DapperAOT copied to clipboard

Mapping inherited class properties

Open shiomax opened this issue 2 years ago • 5 comments

Right now when using a base class for your entities like this

public abstract class EntityBase
{
    public long Id { get; set; }
}

public class Entity1 : EntityBase
{
    public string Name { get; set; }
}

Then the Id property will not be read by the code generated.

It only works when putting the Id directly into Entity1 (otherwise it never gets mapped, always default 0 for any query). I think it´s fairly common to want to enforce consistent naming of ids and timestamps. Although, this can be enforced with interfaces too I suppose (doing that now instead).

The above should either work, or a the analyzer should give you a warning about this (in case this is not gonna be implemented anytime soon). I don´t yet know enough about compile time generators to know how hard it is to figure out that there is a base class and walk back up the class tree.

shiomax avatar Dec 02 '23 01:12 shiomax

agreed, this should work; will get to it

mgravell avatar Dec 13 '23 17:12 mgravell

Hello, when will this fix be released, thanks?

JerryJian avatar Mar 04 '24 09:03 JerryJian

This still doesn't work for me using the latest main branch as of today. @mgravell Should I open a new issue?

public abstract class DataModelBase
{
    public int DataLoadId { get; set; }
    public int LineNum { get; set; }
}

public sealed class TestModel : DataModelBase
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

IAsyncEnumerable<TestModel> test1 = null;
var readerTest = TypeAccessor.CreateDataReader(test1, exact: true);

Below is from the TypeAccessor generated code:

Image

Dean-NC avatar Feb 07 '25 17:02 Dean-NC

I attached the VS debugger, and wherever TypeSymbol.GetMembers() is called, including in the Roslyn code itself, it doesn't see the inherited properties.

Dean-NC avatar Feb 07 '25 19:02 Dean-NC

Thanks for reopening this. I've checked again with version 1.0.48, but it's the same problem and result. I also tested with the base class being regular and not abstract, but same result. It seems this is a TypeAccessor generation issue.

Dean-NC avatar Apr 30 '25 19:04 Dean-NC