Articles in this section
Category / Section

Integrate backend database with ASP.NET MVC QueryBuilder | Setup Guide

2 mins read

The functionality of your application can be improved by integrating a backend database with the ASP.NET MVC QueryBuilder component, which enables dynamic querying of persistent data. This guide provides instructions on how to configure the QueryBuilder component to collaborate with a database using Entity Framework in an ASP.NET MVC application.

Setting up the QueryBuilder component

Initially, the QueryBuilder component must be defined in your Index.cshtml view. Here is an illustration of how to configure the QueryBuilder using a limited number of columns:

<div class="col-lg-12 control-section">
    @Html.EJS().QueryBuilder("querybuilder").DataSource(ds => ds.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor")).Width("auto").Columns(col =>
    {
        col.Field("ContactName").Label("Contact Name").Type("string").Add();
        col.Field("CompanyName").Label("Company Name").Type("string").Add();
        col.Field("City").Label("City").Type("string").Add();
    }).Render()
</div>

Configuring the controller

In the HomeController.cs file, you will be required to establish actions for managing data operations. Here is an illustration of how to configure the controller to interact with the database.

public class HomeController : Controller
{
    NORTHWNDEntities1 db = new NORTHWNDEntities1();

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult UrlDatasource(DataManagerRequest dm)
    {
        IQueryable DataSource = db.C30000Records.AsQueryable();

        QueryableOperation operation = new QueryableOperation();
        // Perform filtering, searching, sorting, and paging
        // ...

        int count = DataSource.Cast<C30000Records>().Count();
        var countData = Json(new { result = DataSource, count = count }, JsonRequestBehavior.AllowGet);
        countData.MaxJsonLength = int.MaxValue;
        return dm.RequiresCounts ? countData : Json(DataSource, JsonRequestBehavior.AllowGet);
    }
}

Database context configuration

Please provide a definition of your database context in the Northwindcontext.cs file in order to establish a mapping between your database tables and entities.

public partial class NORTHWNDEntities1 : DbContext
{
    public NORTHWNDEntities1()
        : base("name=NORTHWNDEntities1")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<C30000Records> C30000Records { get; set; }
}

Connection String Setup

Please make sure that your connection strings are correctly configured in your Web.config file.

<connectionStrings>
    <add name="NORTHWNDEntities1" connectionString="metadata=res://*/Models.NorthWnd.csdl|res://*/Models.NorthWnd.ssdl|res://*/Models.NorthWnd.msl;provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\NORTHWND.MDF;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>

Sample Project

You are able to access a sample project that showcases the synchronization of the Syncfusion QueryBuilder with a backend database by downloading it from the provided link.

Kindly be aware that the code snippets and configurations provided are merely examples and may require modification to suit the specific needs of your application.

Additional Resources

For more detailed information and examples, you can refer to the following resources:

Conclusion

I hope you enjoyed learning about how to integrate backend database with ASP.NET MVC QueryBuilder | Setup Guide.

You can refer to our ASP.NET MVC Query Builder feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Query Builder example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied