Asked by:
Entity Framework & Custom Validation / Custom Display

Question
-
User-1706302664 posted
Good evening all,
I am trying to implement custom valiation on some fields in my web application using EF.
I have tried creating a vb file in the App_Code folder extending the class of the table in question using the table name Opportunities:
Namespace Lollipop_Sales_PipeLineModel
<MetadataType(GetType(OpportunitiesMetaData))> _
Partial Public Class Opportunities
Private Sub OnClosedChanging(ByVal value As String)
If (Char.IsLower(value(0))) Then
Throw New ValidationException("Product names must begin with an upper case character.")
End If
End Sub
Private Sub OnClosed_ReasonChanging(ByVal value As String)
If (Char.IsLower(value(0))) Then
Throw New ValidationException("Product names must begin with an upper case character.")
End If
End Sub
End Class
Public Class OpportunitiesMetaData
<DisplayFormat(DataFormatString:="{0:yyyy-MM-dd}")> _
Public Open_Date As Object
<DisplayFormat(DataFormatString:="{0:yyyy-MM-dd}")> _
Public Est_Closed_Date As Object
<DisplayFormat(DataFormatString:="{0:C}")> _
Public Est_Revenue As Object
<DataTypeAttribute(DataType.Html)> _
Public Description As Object
<DataTypeAttribute(DataType.MultilineText)> _
Public Comments As Object
End Class
End NamespaceUnfortunatley I do not see any changes in display or in the update of the table.
Any advice to get my past this problem would be of great help.
Kind Regards
Yoshima UK
Friday, August 23, 2013 6:26 PM
All replies
-
User-330204900 posted
Hi YoshimaUK, is this Dynamic Data or just Web Forms with EF?
Sunday, August 25, 2013 12:09 PM -
User-1706302664 posted
Sjnaughton, Thanks for the speedy reply. I am not sure of the difference. To explain I created the site by creating the database and then using Visual Studio create the site from the DB. Does that help? I have created some custom pages, other than that my site driven by the EF templates. Kind Regards YoshimaSunday, August 25, 2013 8:18 PM -
User-330204900 posted
Dynamic Data is aspecific Web Project Type ort you could be using the EnableDynamicData on you DataControls?
Monday, August 26, 2013 5:17 AM -
User-1706302664 posted
If i use the standard edit.aspx template what changes should i make to enable dynamic data on the controls? would this then allow my custom validation file to be used?Monday, August 26, 2013 7:23 AM -
User-330204900 posted
if you said
If i use the standard edit.aspx template what changes should i make to enable dynamic data on the controls?this suggest you are using a Dynamic Data project template yes?Monday, August 26, 2013 10:59 AM -
User-1706302664 posted
Sounds right to me. I only use the templates when i need to customize an entity and how it displays in list or detail mode. Otherwise i use the scaffolding only. what do i need to do to get the validation working? i feel i am close.Monday, August 26, 2013 11:07 AM -
User-330204900 posted
First question to get you working :) do any of the attribute in you buddy/metadata class work i.e. other that the validators? I only ask this as the buggest issue I see is medata that is not seen dure to namespace issues etc.
Monday, August 26, 2013 11:25 AM -
User-1706302664 posted
The opportunities.vb file doesn't get hit on local debug in list or edit. It does get parsed for errors, but the code doesn't fire when i use list or edit. should i be making any other code changes in the templates or just the addition of the templates opportunities.vb file in the app folder?Monday, August 26, 2013 12:18 PM -
User-330204900 posted
OK this look like the common issue then, first of all is your data model in a seperate class library from your main webg project?
Als is you web site a website or web application project the latter will have designer.vb files for each page or user control.
Monday, August 26, 2013 12:22 PM -
User-1706302664 posted
My data model file is in the app code folder, ModelDesigner.vb. if you mean my custom pages for different entities like customer or opportunity yes for list.aspx there is a list.aspx.vb for that file. And it is a websiteMonday, August 26, 2013 12:40 PM -
Wednesday, August 28, 2013 3:49 AM
-
User-227760790 posted
hi YoshimaUK ,
here a Custom Validation link is helpful for you:
http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx
hope it can help you.
Wednesday, August 28, 2013 4:53 AM -
User-330204900 posted
The first thing you need to do is verify if ANY of your metadata is being used, try setting DisplayName or some other visual element and ted that if that doe snot work your issue is you metadata/buddy classes. Please post your full metadata classes I need to see the full thing to work out what is happening.
Wednesday, August 28, 2013 4:57 AM -
User-1706302664 posted
Sjnaughton,
this is my Opportunities.vb file in the app_code folder:
Namespace Lollipop_Sales_PipeLine_Model
<MetadataType(GetType(OpportunitiesMetaData))> _
Partial Public Class Opportunities
Private Sub OnClosedChanging(value As Global.System.Boolean)
If (value) Then
MsgBox("test")
End If
End Sub
Private Sub OnClosed_ReasonChanging(value As Global.System.String)
End Sub
End Class
Public Class OpportunitiesMetaData
<DisplayFormat(DataFormatString:="{0:yyyy-MM-dd}")> _
Public Open_Date As Nullable(Of Global.System.DateTime)
<DisplayFormat(DataFormatString:="{0:yyyy-MM-dd}")> _
Public Event_Date As Nullable(Of Global.System.DateTime)
<DisplayFormat(DataFormatString:="{0:C}")> _
Public Est_Revenue As Object
<DataTypeAttribute(DataType.Html)> _
Public Description As Object
<DataTypeAttribute(DataType.MultilineText)> _
Public Comments As Object
End Class
End NamespaceAfter removing and readding the namespace it seems to work now, the display format for Open_Date & Event_Date is working in list. I am not sure what has changed, I am guessing the namespace!?
Moving on I am now trying to implement validation with OnClosedChanging a Boolean (checkbox), you can see that I have included the sub for this inside the Partial Public Class Opportunities. My problem at the moment is that sub is fired all the time, in list and in edit, I get 9 MsgBoxes.
Why is the sub firing all the time in list and edit?
Friday, August 30, 2013 11:26 AM -
User-330204900 posted
Hi YoshimaUK, I'm no expert on EF so I would head over to the EF forums for this I don't use that as I could not get it to work the way I expected it to work I now hook into the saving changes event and that does it for me :)
Saturday, August 31, 2013 6:29 AM