I am working on several providers such as F # to replace some generation of code, and I am having some problems with provided types that extend the base type. For example, one of the providers is the Entity Framework provider, where the metadata for the database schema comes from an external source, and I provide a type that uses DbContext as the base type. This type has properties for accessing various members of the DbSet <table, but when I try to access these properties, I get an Incorrect instance type. Parameter name: obj error Incorrect instance type. Parameter name: obj Incorrect instance type. Parameter name: obj .
I believe this is due to the 'this' parameter (the first argument to GetterCode and SetterCode of the property provided), which is actually of type DbContext, not my derived type. This makes sense since my ProvidedConstructor for the context type calls the DbContext constructor, but I'm not sure what else it can do. I can only guess that I misunderstand how types are created. My code to determine the type, constructor, and property is as follows. Please let me know if I am doing something wrong.
let contextType = ProvidedTypeDefinition("MyContext", Some typeof<DbContext>) let dbContextCtor = typeof<DbContext>.GetConstructor([|typeof<string>|]) let defaultCtor =
...
// Add a DbSet field and property for each table to the context type for providedType in providedTableTypes do let fieldType = typedefof<DbSet<_>>.MakeGenericType([|providedType|]) let dbSetField = ProvidedField(sprintf "_%s" providedType.Name, fieldType) let dbSetProperty = ProvidedProperty(providedType.Name, fieldType, GetterCode = (fun args -> Expr.FieldGet(args.[0], dbSetField)), SetterCode = fun args -> Expr.FieldSet(args.[0], dbSetField, args.[1])) dbSetField.SetFieldAttributes(FieldAttributes.HasDefault) contextType.AddMember dbSetField contextType.AddMember dbSetProperty
Table types are generated and DbSet properties are added to the context type, but when I try to access one of the properties, I get an Incorrect instance type error when the GetterCode expression is GetterCode .
f # entity-framework type-providers
Aaron M. Eshbach
source share