Binding EF Core

Hi There,
Was wondering if Server Side Binding to EF Core, meant there was no code to write via the automatic DataGrid CRUD?
I've tried to get this working but am stuck with not having the data persist back to the database.
I see another question where the anwser was to create an OnActionBegin handler with the CRUD code inside - am I mistaken in thinking the Datagrid had automatic Crud for databound items built in?
Just wondering if there was a downloadable blazor server sample of a DataGrid bound to Northwind... Orders (for example) and had a working sample of Insert, Update, Delete?
Thanks!

7 Replies

RN Rahul Narayanasamy Syncfusion Team April 13, 2020 10:58 AM UTC

Hi Steven, 

Greetings from Syncfusion. 

Query: Binding EF Core (With CRUD operations) 

We have validated your query and created a sample based on your requirement(EF core with CRUD operations). Find the below code snippets and sample for your reference. 

 
<SfGrid TValue="Order" ID="Grid" AllowSorting="true" AllowFiltering="true" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })"> 
        <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> 
        <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings> 
        <GridColumns> 
            <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsIdentity="true" IsPrimaryKey="true" Width="120"></GridColumn> 
            . . . 
       </GridColumns> 
    </SfGrid> 
 
    @code { 
        public SfGrid<Order> GridName { get; set; } 
        // Implementing custom adaptor by extending the DataAdaptor class 
        public class CustomAdaptor : DataAdaptor 
        { 
            OrderContext db = new OrderContext(); 
            // Performs data Read operation 
            public override object Read(DataManagerRequest dm, string key = null) 
            { 
                IEnumerable<Order> DataSource = db.Orders; 
                . . . 
               return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
            } 
 
            // Performs Remove operation 
            public override object Remove(DataManager dm, object value, string keyField, string key) 
            { 
                Order ord = db.Orders.Find(int.Parse(value.ToString())); 
                db.Orders.Remove(ord); 
                db.SaveChanges(); 
                return value; 
            } 
 
            // Performs Insert operation 
            public override object Insert(DataManager dm, object value, string key) 
            { 
                db.Orders.Add(value as Order); 
                db.SaveChanges(); 
                return value; 
            } 
            // Performs Update operation 
            public override object Update(DataManager dm, object value, string keyField, string key) 
            { 
                var data = db.Orders.Where(or => or.OrderID == (value as Order).OrderID).FirstOrDefault(); 
                if (data != null) 
                { 
                    data.OrderID = (value as Order).OrderID; 
                    data.CustomerID = (value as Order).CustomerID; 
                    data.EmployeeID = (value as Order).EmployeeID; 
                } 
                return value; 
            } 
        } 
    } 
 
 


 
Reference:  

Please get back to us if you need further assistance. 

Regards, 
Rahul 



TO Tomasz May 19, 2020 11:18 AM UTC

Hi,

I think, there is missing:

            db.Update(data);
            db.SaveChanges();
            return value;

in public override object Update(DataManager dm, object value, string keyField, string key)

and Grid is not refreshing after update od insert new data :(




RN Rahul Narayanasamy Syncfusion Team May 20, 2020 03:18 PM UTC

Hi Tomasz, 

We have validated your query and we modified the sample based on the requirement. Now the CRUD operations are working fine. Find the below sample for your reference. 


Please get back to us if you need further assistance. 

Regards, 
Rahul 



JA Jacques August 21, 2020 07:46 PM UTC

Hello Rahul,

This is exactly what I was looking for. Many thanks for this sample that will save me a lot of time.

What would be needed for the EditMode.Batch to work as well?

Thanks
Jacques


RN Rahul Narayanasamy Syncfusion Team August 24, 2020 04:05 PM UTC

Hi Jacques, 

Greetings from Syncfusion. 

Query: What would be needed for the EditMode.Batch to work as well? 

We have validated your query and your want to perform CRUD operations using Batch mode. You can achieve your requirement by using BatchUpdateAsync method of CustomAdaptor. Find the below code snippets and documentation for your reference. 

// Performs BatchUpdate operation 
        public override async Task<object> BatchUpdateAsync(DataManager dm, object Changed, object Added, object Deleted, string KeyField, string Key, int? dropIndex) 
        { 
            if (Changed != null) 
            { 
                foreach (var rec in (IEnumerable<Order>)Changed) 
                { 
                    db.Entry((Order)rec).State = EntityState.Modified; 
                    db.SaveChangesAsync(); 
                } 
 
            } 
            if (Added != null) 
            { 
                foreach (var rec in (IEnumerable<Order>)Added) 
                { 
                    db.Orders.Add((Order)rec); 
                    db.SaveChangesAsync(); 
                } 
 
            } 
            if (Deleted != null) 
            { 
                foreach (var rec in (IEnumerable<Order>)Deleted) 
                { 
                    db.Orders.Remove(rec); 
                    db.SaveChangesAsync(); 
                } 
 
            } 
            //return Orders; 
            return Task.Run(() => 
            { 
                return db.Orders; 
            }); 
        } 

Reference: 

Please let us know if you have any concerns. 

Regards, 
Rahul 



BY Byron January 14, 2021 12:56 AM UTC

Why is it necessary to use a controller and an API/HTTP call to get the data for the grid? To me, that is not "binding to EF".

Can we do everything like this but using the dbContext and EF to get the initial data without going through HTTP > API Controller > Context > JSON ?


JP Jeevakanth Palaniappan Syncfusion Team January 14, 2021 12:13 PM UTC

Hi Byron, 

We have prepared a sample with dbContext and EF without the controller. Please refer the below sample for your reference. 


Note: Please change the connection string in Data/ordercontext before running the sample. 

Get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 
 


Loader.
Up arrow icon