This is a migrated thread and some comments may be shown as answers.

Any samples that user Entity Framework NOT Declarativley for CRUD operations?

7 Answers 159 Views
Grid
This is a migrated thread and some comments may be shown as answers.
matt
Top achievements
Rank 1
matt asked on 17 Sep 2010, 02:53 PM
The declaritive way is great but I have a scenerio for example that I have a list of organizations.. and adding and deleting all work great..

But when I update or create a new item I also need to programmtically get the UserID and insert that as well... so because of that it seems its breaks the whoel declaritive way of doing things right?

So I am searching for some EF/RadGrid/code behind data access sample. anyone ?

7 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 23 Sep 2010, 09:33 AM
Hello Matt,

Please check the attached sample project and let me know if you need further assistance.

Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
matt
Top achievements
Rank 1
answered on 23 Sep 2010, 03:04 PM
thanks. that is a good start..

But what about if I have a column called 'UpdateBy' and when I update a 'Organization' table I also want to save the person who updated..

So how do I assign 'UpdateBy' a value programmatically... then have it update that field in the 'Organization' table in addition to what it is already updating ?

0
matt
Top achievements
Rank 1
answered on 23 Sep 2010, 04:07 PM
I am getting close.. I can update values saying

(editItem[

 

"UpdateDate"].Controls[0] as TextBox).Text = DateTime.Now.ToString();

 



But what if I do not what that field to show in the details view? I can ake it READ ONLY but then I can't change that value?

How do I hide that field yet be able to change it programmatically ?

0
Cori
Top achievements
Rank 2
answered on 23 Sep 2010, 05:33 PM
Hello Matt,

Instead of adding it as a column to the RadGrid, since your not going to show it. You can update it in the EntityDataSource Updating event. In that event you do something like this:

protected void EntityDataSource1_Updating(object sender, EntityDataSourceChangingEventArgs e)
    {
       Organization org = (Organization)e.Entity;
       org.UpdateBy = "[UserID]";
    }

So pretty much all it does is get the Entity that is about to be inserted and casts it to the correct type, Organization in this case. From there you just set the properties that you want.

You can use the same code for the Inserting event as well.

I hope that helps.
0
matt
Top achievements
Rank 1
answered on 27 Sep 2010, 11:18 PM
So I guess it should go through the RadGrid update event as well as the EntityDataSource event?  It never seems to hit the

EntityDataSource1_Updated

breakpoint...

So what I am trying to do is update a column that does not exist in the grid but DOES exist in the Entity (updateby)...

Actually it seems like I am not even using the entitydatasource...

How should I access that field???  Does the simple CRUD code below seem right?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
  
namespace Thc.Gms.Web.UI.ProtoMatt
{
    public partial class EFTLTestOrganizations : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
  
        }
  
        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editItem = e.Item as GridEditableItem;
          
          //  (editItem["UpdateBy"].Controls[0] as TextBox).Text = "666";
          //  (editItem["UpdateDate"].Controls[0] as TextBox).Text = DateTime.Now.ToString();
  
            using (var dc = new G3TEntities())
            {
                var orgID = (int)editItem.GetDataKeyValue("OrgID");
                //  string EmployeeID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"].ToString();
                var org = dc.Orgs.Where(p => p.OrgID == orgID).FirstOrDefault();
                editItem.UpdateValues(org);
                dc.SaveChanges();
            }
        }
  
        protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            using (var dc = new G3TEntities())
            {
                GridDataItem item = (GridDataItem)e.Item;
                int OrgID = Int32.Parse(item.OwnerTableView.DataKeyValues[item.ItemIndex]["OrgID"].ToString());
                var org = dc.Orgs2.Where(p => p.OrgID == OrgID).First();        
                dc.DeleteObject(org);
                dc.SaveChanges();
            }
        }
  
        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editItem = e.Item as GridEditableItem;
            (editItem["CreatedBy"].Controls[0] as TextBox).Text = "111";
  
            using (var dc = new G3TEntities())
            {
                var org = new Orgs2();
                editItem.UpdateValues(org);
                dc.AddToOrgs2(org);
                dc.SaveChanges();
            }
        }
  
        protected void EntityDataSource1_Updated(object sender, EntityDataSourceChangedEventArgs e)
        {
            Org oOrg = new Org();
            oOrg = (Org)e.Entity;
            oOrg.updateby = 69;
        }
    }
}

0
matt
Top achievements
Rank 1
answered on 27 Sep 2010, 11:19 PM
Maybe I shoudlnt both usesing entitydatasrouce... It seems its only good in cases where you are doing very simple 1-to-1 updates..   or am I mistaken?

0
Accepted
Daniel
Telerik team
answered on 01 Oct 2010, 04:32 PM
Hello Matt,

The Updating event won't be triggered in this case since you are not using the EntityDataSource for updating. Instead, you can use the UpdateCommand event. Below you can find a sample code:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem editItem = e.Item as GridEditableItem;
    using (NorthwindEntities1 myContext = new NorthwindEntities1())
    {
        var categoryId = (int)editItem.GetDataKeyValue("CategoryID");
        var category = myContext.Categories.Where(p => p.CategoryID == categoryId).FirstOrDefault();          
 
        editItem.UpdateValues(category);
 
        category.Description = "Test"; //update the Description property manually
 
        myContext.SaveChanges();
    }
}

Best regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
matt
Top achievements
Rank 1
Answers by
Daniel
Telerik team
matt
Top achievements
Rank 1
Cori
Top achievements
Rank 2
Share this question
or