Entity Framework 6.1 using Power Tools 4 generates an error when using the command:
View Entity Data Model (Read Only) on the context.
The error:
An error occurred while trying to build the model for Context. See the output window for details.
The output window basically states:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Method not found: 'xyz.domain.User xyz.domain.DomainBase.get_ModifiedBy()'
* CRITICAL * The power tools are also caching the schema and do NOT update with changes! ***
Example:
Create a base class:
public abstract class DomainBase {
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public User CreatedBy { get; set; }
public string CreatedByName { get; set; }
public User ModifiedBy { get; set; }
public string ModifiedByName { get; set; }
}
Create a class that inherits from the base class:
public class User : DomainBase {
public int UserId { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public string Salt { get; set; }
public string Pin { get; set; }
}
Create Base Mapping:
public class DomainBaseConfig<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : DomainBase {
public DomainBaseConfig() {
this.HasOptional(c => c.ModifiedBy).WithOptionalDependent()
.Map(m => m.MapKey("ModifiedById"));
this.HasOptional(c => c.CreatedBy).WithOptionalDependent()
.Map(m => m.MapKey("CreatedById"));
this.Property(t => t.CreatedByName).HasMaxLength(FieldMaxSize.FullName).IsRequired();
this.Property(t => t.ModifiedByName).HasMaxLength(FieldMaxSize.FullName).IsRequired();
this.Property(t => t.DateCreated).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
this.Property(t => t.DateModified).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
}
}
Created User class mapping:
public UserConfig() {
this.ToTable("Users");
this.HasKey(c => c.UserId);
this.Property(t => t.FirstName).HasMaxLength(FieldMaxSize.FirstName).IsRequired();
this.Property(t => t.LastName).HasMaxLength(FieldMaxSize.LastName).IsRequired();
this.Property(t => t.Password).HasMaxLength(FieldMaxSize.Password).IsRequired();
this.Property(t => t.Pin).HasMaxLength(FieldMaxSize.Pin).IsRequired();
this.Property(t => t.Salt).HasMaxLength(FieldMaxSize.Salt).IsRequired();
this.Property(t => t.Username).HasMaxLength(FieldMaxSize.Username).IsRequired().HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_Username", 1) { IsUnique = true }));
}
When trying to view the model, the error seems to be related to the base class referring to the parent class where the parent requires the base (they reference each other) and the designer doesn't like this, but EF handles it just fine.
PROOF: If I remove the references to "User" in the base class, the designer WILL work (badly)... NOTE... I do get it to work by removing both base class properties referencing the User class and their associated mappings... BUT... the designer STILL MODELS THEM!
So, I remove the User properties (ModifiedBy and CreatedBy) in the base class and I also remove the associated mappings, then the designer builds a graphic representations of the schema ... BUT... it still includes the properties I just commented out (they are clearly cached somewhere)!
PLEASE!
(1) Correct the designer so it doesn't crash on valid schema as provided in the sample (2) Fix the caching problem with the designer so that it NEVER builds schema for the designer on old/stale code!
Based on this example, the Power Tools designer is completely unreliable as it can clearly build a model that DOES NOT match actual code/schema.