Invalid instance type for derived type in F # Erased Type Provider

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 = // Use static parameter 'sqlConnection' ProvidedConstructor(List.Empty, BaseConstructorCall = (fun args -> dbContextCtor, args), InvokeCode = fun args -> Expr.NewObject(dbContextCtor, [ <@@ sqlConnection @@> ])) let stringCtor = ProvidedConstructor([ProvidedParameter("sqlConnection", typeof<string>)], BaseConstructorCall = (fun args -> dbContextCtor, args), InvokeCode = fun args -> Expr.NewObject(dbContextCtor, [ args.[1] ])) contextType.AddMember(defaultCtor) contextType.AddMember(stringCtor) 

...

 // 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 .

+7
f # entity-framework type-providers
source share

No one has answered this question yet.

See related questions:

392
Failed to load Entity Framework provider type?
38
F # how they work
14
Can I provide a type as input for a type provider in F #?
12
F # type provider - "only returned generated types"
nine
F # Type Providers and Data Processing
5
Intellisense does not work for a provider like F #
3
F # type provider
3
F # How to create an instance of a provided type
2
Printing F # Supplier Types
one
How to use DbContext if there is no DbSet <> in it, for request?

All Articles