Answered by:
Missing data annotation for ForeignKey

Question
-
User539648229 posted
Hi
I'm trying to use a foreign key with an EntityTemplate. In the none EntityTemplate version that is generated with Webforms Scaffolding tool, a foreign key is used with a DynamicControl with DataTypeName, DataValueField, DataTextField and UIHint attributes and it works fine. It seems that I must provide these data(with some annotation) in my model(code first) to be used with EntityTemplate. Now without providing these data there is the exception "'CalendarId' is not a foreign key column and cannot be used here." in ForeignKey_Edit.ascx. What annotation should I use?
Thanks
Saturday, December 26, 2015 8:21 AM
Answers
-
User539648229 posted
Hi
As I said Table.GetScaffoldColumns() in the Entity template file does not return navigation properties. I used FK field plus the following in ForeignKey_Edit for overcome the error('CalendarId' is not a foreign key column and cannot be used here.):
protected override void ExtractForeignKey(IDictionary dictionary, string selectedValue) { if (Column is MetaForeignKeyColumn) base.ExtractForeignKey(dictionary, selectedValue); else if (Column is MetaColumn && Column.HasAttribute<ForeignKeyAttribute>()) dictionary.Add(Column.Name, selectedValue); else throw new NotSupportedException("The column must be 'MetaForeignKeyColumn' or 'MetaColumn' with ForeignKey Attribute."); }
It works now. Thanks anyway.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 5, 2016 12:00 PM
All replies
-
User-330204900 posted
Hi IranianCurio, you don't need any more that the field name if you are uysing the Entity framework then you can easily see the foreign key field in the navigation properties of the entity you use that name in the DataField property and then set the mode using the mode property like below
<asp:DynamicControl ID="DynamicControl1" runat="server" DataField="Type" Mode="Edit" />
the Fk Fiuedl would notmaly be called TypeId in my naming convention but the ForeignKey field would be named after the FK table in this case Type.
Hope that helps.
Monday, December 28, 2015 12:27 AM -
User539648229 posted
Hi
I guess you mean that I use navigation property(Calendar) instead of FK(CalendarId). At first I used "Calendar" but it didn't work and no control was appeared for Calendar and a Textbox appeared for CalendarId. The reason was that Table.GetScaffoldColumns() in the Entity template file does not return navigation properties. I compared this behavior with the behavior in the Dynamic Data template and the difference was in FormView1.SetMetaTable() in the Init of the page. I added it and now a DropDownList appears for Calendar and but still a Textbox for CalendarId. Even when I Used [Display(AutoGenerateField = false)] for CalendarId there is no Textbox but the a validation message appears "CalendarId is required". Complex situation! Help please.
Thanks
Monday, December 28, 2015 6:59 AM -
User1686483761 posted
Hi IranianCurious,
I guess you mean that I use navigation property(Calendar) instead of FK(CalendarId). At first I used "Calendar" but it didn't work and no control was appeared for Calendar and a Textbox appeared for CalendarId. The reason was that Table.GetScaffoldColumns() in the Entity template file does not return navigation propertiesIn my experience, Templates doesn't use data annotations because classes generated from templates don't need them. EF also don't need them because mapping is defined in XML files not in code. If you need data annotations you must either:
- Modify template to use them (this requires understanding of EF metadata model)
- Don't use templates and use code first instead
Besides, you could refer to the following link to display a navigation property.
http://forums.asp.net/t/1730820.aspx?How+can+i+display+a+navigation+property+in+a+grid+view+control+
Best Regards,
sudo
Wednesday, December 30, 2015 8:11 AM -
User539648229 posted
Hi
I think there is a misunderstanding. I don't know what do you mean with templates but I mean Entity Templates. In these templates a Dynamic Control is added for each field and this control renders the appropriate control based on data type of the field and metadata provided for it. Also I use Code First.
Wednesday, December 30, 2015 8:36 AM -
User-330204900 posted
Yes that is correct :D you use the navigation property
Wednesday, December 30, 2015 3:21 PM -
User-330204900 posted
Hi Sudo10, this is Dynamic Data and it does use Page Field and Entity Templates :)
Wednesday, December 30, 2015 3:23 PM -
User539648229 posted
Hi sjnaughton
I didn't exactly get what you mean.
Also:
Happy New Year! I wish you an excellent year for you and your family in 2016.
Thursday, December 31, 2015 3:35 PM -
User-330204900 posted
Hi Ali, what I mean is when you are creating a EntityTemplate it is for a specific entity i.e. the Person entity and if the person entity has a FK relationship with say the Title table using the FK column TitleId to Title.Id you don't specify the FK Column but the Navigation Property
<asp:DynamicControl ID="DynamicControl1" runat="server" DataField="Title" Mode="Edit" />
In my example you specify the Title "Navigation Property" to get the correct field template to display in the EntityTemplate.
Hope this helps, maybe i have not understood what you want to achieve :_
Monday, January 4, 2016 12:38 PM -
User539648229 posted
Hi
As I said Table.GetScaffoldColumns() in the Entity template file does not return navigation properties. I used FK field plus the following in ForeignKey_Edit for overcome the error('CalendarId' is not a foreign key column and cannot be used here.):
protected override void ExtractForeignKey(IDictionary dictionary, string selectedValue) { if (Column is MetaForeignKeyColumn) base.ExtractForeignKey(dictionary, selectedValue); else if (Column is MetaColumn && Column.HasAttribute<ForeignKeyAttribute>()) dictionary.Add(Column.Name, selectedValue); else throw new NotSupportedException("The column must be 'MetaForeignKeyColumn' or 'MetaColumn' with ForeignKey Attribute."); }
It works now. Thanks anyway.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 5, 2016 12:00 PM -
User-330204900 posted
hi Ali, glad you are working but still not sure what you are trying to achieve. If the FK Navigation Property is there in the model and it has not been set to be hidden is some way then
Table.GetScaffoldColumns()
will return it, there must be some other issue.
Tuesday, January 5, 2016 1:14 PM -
User539648229 posted
Hi
If you use Web Form Scaffolding tool it uses FK instead of Navigation. I think under the situation I described Table.GetScaffoldColumns() returns FK and not Navigation.
Tuesday, January 5, 2016 3:27 PM -
User-330204900 posted
OK so you are using Scaffolding not Dynamic Data then? Ah!
Tuesday, January 5, 2016 3:40 PM