Controlling column mapping with a custom convention in Entity Framework 6

c# entity-framework entity-framework-6

Question

I'm in a class.TypeEntity It will serve as the foundation class for a large number of entities. Considering that I'm using TPC, I must map every property on the base class to the table containing the name of the concrete class, set theKey field to be created by a database.

I'm doing this right now using anEntityTypeConfiguration For each of the following entity types:

class LineItemType : EntityTypeConfiguration<Models.LineItemType>
{
    public LineItemType()
    {
        this.Property(e => e.Key)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Map(e => e.MapInheritedProperties()
                       .ToTable(nameof(LineItemType)));
    }
}

Although it is highly repetitious, this works good. I must keep in mind to create a configuration class for each type that descended fromTypeEntity Map the inherited properties, then set the key. This looks to be the perfect situation for a customConvention .


I developed aTypeEntityTpcConventionConvention the following

class TypeEntityTpcConvention : Convention
{
    public TypeEntityTpcConvention()
    {
        this.Types<TypeEntity>()
            .Configure(e => e.Property(p => p.Key)
                             .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity));
    }
}

It may be used to setKey I can't find a method to access the table mappings for properties from within a convention, even though they are described as database produced.


I'd hope for something along these lines:

this.Types<TypeEntity>()
    .Configure(e => e.MapInheritedProperties()
    .ToTable(e.ClrType.Name));

For every property that has to be mapped, you could even make a call like this:

this.Types<TypeEntity>()
    .Configure(e => e.Property(p=>p.Key)
                     .ToTable(e.ClrType.Name));

Both of which don't appear to exist. Is there any way for me to manage the property mapping from aConvention ?


Upon more investigation, it seems that there are more sophisticated convention alternatives accessible likeIStoreModelConvention and IConceptualModelConvention yet there is a great dearth of helpful guidance on how to utilise them. I spent hours poring over these interfaces with breakpoints and monitor windows, but I still haven't worked out how to manage column mapping using them.


My current workaround is to locate all kinds that derive from a certain class using reflection.TypeEntity in OnModelCreating Select the appropriate database, then map the attributes to it. This works, but if at all possible, I would like to use a convention as it looks like this is exactly the kind of situation for which they were designed. I feel as if I am overlooking something simple.

1
9
9/14/2017 1:25:09 PM

Popular Answer

ZZZ_tmp
8
9/14/2017 2:33:51 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow