Search code examples
breezebreeze-sharp

ModelValidationException when models inherit from BaseEntity


I have some models classes that inherit from a single abstract class, which in turn is deriving from BaseEntity

public abstract class Item: BaseEntity

When I try create an Entity Data Model on my context I get the following error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

DataLayer.EntityAspect: : EntityType 'EntityAspect' has no key defined. Define the key for this EntityType. DataLayer.EntityKey: : EntityType 'EntityKey' has no key defined. Define the key for this EntityType. DataLayer.MetadataStore: : EntityType 'MetadataStore' has no key defined. Define the key for this EntityType. DataLayer.Validator: : EntityType 'Validator' has no key defined. Define the key for this EntityType. DataLayer.DataType: : EntityType 'DataType' has no key defined. Define the key for this EntityType. DataLayer.StructuralType: : EntityType 'StructuralType' has no key defined. Define the key for this EntityType. DataLayer.ValidationError: : EntityType 'ValidationError' has no key defined. Define the key for this EntityType. DataLayer.ValidationContext: : EntityType 'ValidationContext' has no key defined. Define the key for this EntityType. DataLayer.StructuralProperty: : EntityType 'StructuralProperty' has no key defined. Define the key for this EntityType. EntityAspects: EntityType: EntitySet 'EntityAspects' is based on type 'EntityAspect' that has no keys defined. EntityKeys: EntityType: EntitySet 'EntityKeys' is based on type 'EntityKey' that has no keys defined. MetadataStores: EntityType: EntitySet 'MetadataStores' is based on type 'MetadataStore' that has no keys defined. Validators: EntityType: EntitySet 'Validators' is based on type 'Validator' that has no keys defined. DataTypes: EntityType: EntitySet 'DataTypes' is based on type 'DataType' that has no keys defined. StructuralTypes: EntityType: EntitySet 'StructuralTypes' is based on type 'StructuralType' that has no keys defined. ValidationErrors: EntityType: EntitySet 'ValidationErrors' is based on type 'ValidationError' that has no keys defined. ValidationContexts: EntityType: EntitySet 'ValidationContexts' is based on type 'ValidationContext' that has no keys defined. StructuralProperties: EntityType: EntitySet 'StructuralProperties' is based on type 'StructuralProperty' that has no keys defined.

at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate() at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized() at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.DbContextPackage.Handlers.ViewContextHandler.ViewContext(MenuCommand menuCommand, Object context, Type systemContextType)

If I remove the BaseEntity inheritance and create an Entity Data Model, it completes successfully. What is BaseEntity doing to break my model?

The real world problem is that when I am attempting to return the metadata from the DataService.Metadata() call it hits this error too.

Any ideas how to get around this?

Thanks


Solution

  • Ended up using a "hack" whereby my server side entity models did not derive BaseEntity, but my client side entity models did. I accomplished this by using partial classes, and file links in VS