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

Entity Framework and Foreign Key Relationship

7 Answers 177 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 23 Mar 2012, 08:12 PM
I have a radgrid populated by an entitydatasource using a where paramter to limit the return to a predetermined Identity Value. If a user wants to add a new record to this collection how can I keep that identity value? In this case, the value is a foreign key on a many-to-one relationship - in this case the site of a project - so I have projectId and want to use that projectId as the value in the new record that is created. A user shouldn't be able to select this value because they already did.

I've tried using defaultvalue and that works, but it displays in the insert form and I don't want that. If I use ReadOnly it doesn't get used and my insert fails because of foreign key constraints.

I don't want to handle it in code because I'm writing everything declaratively - is there a way to do this?

7 Answers, 1 is accepted

Sort by
0
Adam
Top achievements
Rank 1
answered on 23 Mar 2012, 08:37 PM
In case anyone else winds up here - there's a pretty ugly hack but it does work:

First, in PageLoad:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EntitySource.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.Int32, DefaultValue = "1", Name = "SiteId" });
                ((Telerik.Web.UI.GridBoundColumn)RadGrid1.MasterTableView.GetColumn("SiteId")).DefaultInsertValue = "1";
            }
        }

handle ItemDataBound like this:

protected void ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
            {
                GridEditFormItem edititem = (GridEditFormItem)e.Item;
                TextBox txtbx = (TextBox)edititem["SiteId"].Controls[0];
                txtbx.Visible = false;
            }
        }


I suspect you can hide the label this way too, but I'm not concerned about that right now.
0
tony
Top achievements
Rank 1
answered on 02 May 2012, 06:58 PM
i am assuming that a lack of response from telerik means that an ugly hack is required. but i have the same scenario except that my table relationship is only 1 to many. it would be nice if there were a clean declarative way to propagate the foreign key to the insert. i am going to see if i can reference a form control value, e.g. the text value of a label control, to populate the defaultvalue of the foreign key column. but this has the ugly drawback of having to select the column - i hope that making it invisible does not screw anything.
0
Antonio Stoilkov
Telerik team
answered on 07 May 2012, 08:44 AM
Hi Tony,

The described scenario is not common and there is no declarative way of resolving it. However, you could set the data source DefaultInsertValue programatically and set the column ReadOnly property to true.

Regards,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
tony
Top achievements
Rank 1
answered on 07 May 2012, 07:00 PM
the case is quite common and i found a number of frustrated users' encountering it without adequate resolution. the primary key data is available and i should be able to propagate it to a new row with little effort....the parent child relationship is one of the most pervasive in data models of any substance....foreign keys are endemic....

for those having this problem, one solution i found is to create a control holding the parent key which can then be populated into a hidden column child / dependent key during the insert command event. this can be done in 4 lines of code.

after further exploration with the grid and entity framework, i believe cleaner solutions are available but have not had time to develop them.
0
Antonio Stoilkov
Telerik team
answered on 10 May 2012, 08:47 AM
Hello Tony,

We understand your point of view and agree that there is no declarative way of resolving such issues. However, there are custom implementations which could help in achieving the desired scenario. Additionally, we have help articles which describes similar problems. Note that such methods could be wrapped in help classes and used through whole web application.

Kind regards,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
tony
Top achievements
Rank 1
answered on 10 May 2012, 11:02 PM
a PropagatePrimaryKeyForeignKeys property would be lovely but i did find a work around - thank you.
0
Antonio Stoilkov
Telerik team
answered on 15 May 2012, 06:56 AM
Hi Tony,

Thank you for the feature request. I will contact our developers and pass your suggestions. Note that decisions for implementing a feature is based on the controls needs and the customers demand rates.

Greetings,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Adam
Top achievements
Rank 1
Answers by
Adam
Top achievements
Rank 1
tony
Top achievements
Rank 1
Antonio Stoilkov
Telerik team
Share this question
or