0

I have a number of Types that inherit from the Interface 'IEventReportElement', which exposes the property 'IdEventReport':

public interface IEventReportElement
{
    long Id { get; set; }
    long? IdEventReport { get; set; }
}

This is a nullable property, as I'm not always able to fill it out properly right away, but should be not nullable in the database.

Thats why I have added the line

modelBuilder.Types<IEventReportElement>().Configure(x=>x.Property(y=>y.IdEventReport).IsRequired());

to my OnModelCreating method in the DbContext.

However, the Type 'Position' has to implement this interface, but should NOT have the column for property 'IdEventReport' in the database, but instead a column for the property 'IdParent' it exposes.

public class Position : BOBase, IEventReportElement
{
    public long? IdEventReport 
    {
        get { return IdParent; }
        set { IdParent = value; }
    }

    public long? IdParent { get; set; }
}

and the section in the modelBuilder

        modelBuilder.Entity<Position>().Property(x => x.IdParent).IsRequired();
        modelBuilder.Entity<Position>().Ignore(x => x.IdEventReport);

However, this throws an exception already when trying to Create the database:

System.InvalidOperationException: The property 'IdEventReport' is not a declared property on type 'Position'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

Though this may be valid, is it not possible to override the given Type configuration for a specific type? Do I have to add the line .IsRequired() to every other type that implements this interface, or is there another way to overcome this?

DevilSuichiro
  • 1,049
  • 8
  • 21
  • I guess that you try to select `Position.IdEventReport` in query. is this right? – Adil Mammadov Sep 08 '16 at 08:08
  • no, IdEventReport should not be included in the model. IEventReportElement has other important properties I want to use, I just don't want to use this property (in the database). – DevilSuichiro Sep 08 '16 at 08:19

2 Answers2

0

If you just want that the column has a different name in the database, use HasColumnName.

For access to IdParent in the C# model, use [NotMapped] to tell EF to ignore this property when creating the DB.

public class Position : BOBase, IEventReportElement {
    public long? IdEventReport {get; set; }

    [NotMapped]
    public long? IdParent {
        get { return IdEventReport ; }
        set { IdEventReport = value; }
    } 
}

modelBuilder.Entity<Position>().Property(x => x.IdEventReport).HasColumnName("IdParent");

As a side note: why are you implementing an interface that you don't want to use? Maybe you can split the interface in smaller parts and only implement what you are going to use.

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • This is not what I want, as I want to query the table by the IdParent property, this is not possible with this approach. – DevilSuichiro Sep 08 '16 at 08:10
0

I did find a solution, however it's a not so nice one. I did it by modifying the line of the type configuration to

modelBuilder.Types<IEventReportElement>().Where(x=>x.Name!="Position").Configure(x=>x.Property(y=>y.IdEventReport).IsRequired());
DevilSuichiro
  • 1,049
  • 8
  • 21