Search code examples
c#asp.net-mvcentity-frameworkstored-procedurescomplextype

Error: An error occurred while preparing the command definition


I have 2 stored procedures under Function Imports in Model Browser, lets say ProcA and ProcB, each with their own Complex Type, let's say TypeA and Type2, respectively.

TypeA properties:

Int32 SomeId;
String PersonName;
String PersonAddress;

TypeB properties

Int32 SomeId;
String PersonName;
String PersonAddress;
Int32 Age;
Int32 Height;

Everything is good, fully functional with no exception except when I try to nest TypeA into TypeB as TypeB is basically a replica of TypeA plus it has 2 of its own additional properties.

TypeB properties

Int32 Age;
Int32 Height;
TypeA TypeAObj; // nesting typeA here

ProcA works fine but when I call ProcB it throws an exception:

Error: An error occurred while preparing the command definition. See the inner exception for details.

Message = "Nested ComplexType property 'TypeA' in the ReturnType 'TypeB' of the function 'ProcB' is not supported, please consider flattening the nested ComplexType property."

Do i have to use flattened complex types and nesting is not possible in my case?

I'm just trying to reduce the lines of code and force reusability.

Any input on this would be great. Please let me know if there's any more information I can provide.

PS: Error occurs in DB.Context.cs file

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<TypeB>("ProcB", param1, param2);

Thanks


Solution

  • This is not going to work. As of EF 6.0, it's not supported.

    Check out this link. The property pulls out the localized string for the problem behind this error and it still exists in EF 6.0:

    https://msdn.microsoft.com/en-us/library/microsoft.data.entity.design.resources.mappingdetails_errcomplextypepropertiesnotsupported(v=vs.113).aspx#P:Microsoft.Data.Entity.Design.Resources.MappingDetails_ErrComplexTypePropertiesNotSupported

    The version history does not indicate they have addressed this:

    https://msdn.microsoft.com/en-us/library/jj574253(v=vs.113).aspx

    I found a thread going back to 2010 that says "it's in the backlog":

    https://social.msdn.microsoft.com/Forums/en-US/c412a7d7-9455-47bc-87dd-46d3f0809b1b/mapping-a-stored-procedure-to-an-entity-that-contains-complex-types?forum=adodotnetentityframework

    And a quick local test with VS2013 and the latest EF confirms this:

    Quick local test in VS2013 with latest EF confirms this.