سؤال

I'm trying to re-use some of the model configurations on several entities that implements a interface.

Check this code:

public static void ConfigureAsAuditable<T>(this EntityTypeConfiguration<T> thisRef)
            where T : class, IAuditable
        {
            thisRef.Property(x => x.CreatedOn)
                .HasColumnName("utctimestamp")
                .IsRequired();

            thisRef.Property(x => x.LastUpdate)
                .HasColumnName("utclastchanged")
                .IsRequired();
        } // ConfigureAsAuditable

as you can see I'm trying to call the extension method "ConfigureAsAuditable" on my onmodelcreating method like this:

EntityTypeConfiguration<Account> conf = null;

    conf = modelBuilder.Entity<Account>();
    conf.ToTable("dbo.taccount");

    conf.ConfigureAsAuditable();

When debugging i get this exception:

The property 'CreatedOn' is not a declared property on type 'Account'. 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.

Thanks in advance :) PD: I'm using EF 5-rc, VS 2011 and .NET Framework 4.5

هل كانت مفيدة؟

المحلول

I think a better approach would be to implement your own derived version of EntityTypeConfiguration. For example:

    public class MyAuditableConfigurationEntityType<T> : EntityTypeConfiguration<T> 
where T : class, IAuditable{
public bool IsAuditable{get;set;}
}

Then, when building your model, use that new type:

var accountConfiguration = new MyAuditableConfigurationEntityType<Account>();
accountConfiguration.IsAuditable = true; // or whatever you need to set
accountConfiguration.(HasKey/Ignore/ToTable/Whatever)
modelBuilder.Configurations.Add(accountConfiguration);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top