How to Make ComplexType of Dynamic Query in Entity Framework?

This article applies to those developers who use Visual Studio tools and Entity Framework for developing an MVC application.

Here I am going to explain the problem that I faced during the development of an MVC application using Entity Framework.

Problem

Earlier when I write a simple procedure for my application, I easily make the ComplexType without any problem.

But after sometimes I needed to make that procedure dynamic, so I made the change in procedure.

So that, it returns the result of what I expect. But the problem is that the entity framework doesn’t provide me complexType for a dynamic procedure.

When I fire-click on get column information it just displays the message as shown below.

Problem Screen

Entity Framework edit function import dialog showing the stored procedure returns no columns message

Also Read: How to Implement jQuery Ajax Error handling in ASP.NET MVC Project

Solution

After long research and changes, I found the solution. Here below, I listed all the steps for the setup. The steps for solution areas are listed below.

  1. First of all, Make the Dynamic procedure in the SQL server, and execute the procedure, it will display the output as our expectations.
    Dynamic procedure
  2. Now, this step is very important. Comment on the query code that we have written before executing. Go to the message window that will show the query that returns the result that we want, copy it, and paste it inside our procedure, as shown below.
    SQL Server Management Studio showing commented dynamic query code and the message output for creating ComplexType
  3. Now, move to Visual Studio, and add/refresh the entity data model.
  4. Go to a model browser.
  5. Select the procedure from a function.
  6. Double-click or go to the edit window.
  7. Click the get column information button it returns the result of a column, as shown below image. They create a new complex type that will add a complex type to the application.
    Illustration showing step-by-step process flow with icons and labels used for workflow or integration explanation.
  8. Remove the comment from the procedure in which we comment first, put the comment on a query which we use forget result else remove the query code. Picture as shown below.
    SQL Server procedure editor showing the dynamic query restored after ComplexType creation in Entity Framework

If you like this simple solution for creating a complex type of dynamic query, share it on your social media to help other .NET MVC developers, who are facing this type of issue. thanks a lot.

Article by

Chintan Prajapati

Chintan Prajapati is the Founder and CEO of Satva Solutions and a seasoned computer engineer with over two decades of experience in the software industry. His expertise spans Accounting & ERP Integrations, Robotic Process Automation, and the development of technology solutions built around leading ERP and accounting platforms with a particular focus on responsible AI and machine learning in fintech.Chintan holds a BE in Computer Engineering and carries an impressive roster of certifications, including Microsoft Certified Professional, Microsoft Certified Technology Specialist, Certified Azure Solution Developer, Certified Intuit Developer, Certified QuickBooks ProAdvisor, and Xero Developer.Over the course of his career, he has made a measurable impact on the accounting industry consulting on and delivering integration and automation solutions that have collectively saved thousands of man-hours. His writing aims to offer readers practical, insight-driven advice on harnessing technology to unlock greater business efficiency.When he steps away from the desk, Chintan can be found trekking through mountain trails or watching birds in the wild. Grounded in the philosophy of delivering the highest value to clients, he continues to champion innovation and excellence in digital transformation from his home base in Ahmedabad, India.