I have a project in which I have a database model class provided along with a separate EDMX EF model. In the same solution, I have a web service which accesses this project along with the model class.
I want the model class to perform data annotions against the front end for validation, but is not getting validated at all.
For brevity, the model class (in my Model project) is as follows. My web service references this class and is used as the interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel;
using System.Runtime.Serialization;
When I hit the Submit button on the form, it tries to add the record and does not do any validation. A
runtime error occurs informing me that the email address is required. I obvioulsy want this validation
to be done up front with the data annotations.
An actual runtime error is coming back saying
that the email address should not be NULL when the record tries to be added. This
is correct. The database column requires a value.
I thought that by having the data annotations in the model, if there is
something wrong with the front end and the model is not valid once the form
tries to be posted, the corresponding data annotation error should display on
the form. I was under the impression that there is no need for writing any
specific client side validation. The model is supposed to take care of that for
you. Am I incorrect in this assumption?
Alan, I've already viewed that article. It is not set up for the Database First method. It is set up for Code First. The way I have my Model class set up is supposed to be the way to set it up using EntityFramewowrk with the DataBase First methodology. Take
a look at the previous post to see how the class is set up.
Additionally, the content in there is outdated as far as the DLLs go. Check the DLLs I'm using in the above post.
The bottom line is this....
My data model for the Email field specifies the following:
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
The ModelState.Count parameter is correct and the ModelState.Keys are correct along with their respective values that I am checking during debugging in the collection. It's not setting the ModelState.IsValid property to FALSE based on the above directives.
The Model directives above are not being triggered on the front end for validation.
It's amazing how I can't get a simple direct answer of how to use DataAnnotations using the DataBase First methodology...
Make sure that your view (aspx pages in the MVC) has used Validation technology, something looks like this:
e.g:
[PropertiesMustMatch("Password", "ConfirmPassword",
ErrorMessage = "The password and confirmation password do not match.")]
public class RegisterModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Email { get; set; }
[Required]
[ValidatePasswordLength]
public string Password { get; set; }
[Required]
public string ConfirmPassword { get; set; }
}
And your aspx codes should be:
<% using (Html.BeginForm()) { %>
<%= Html.ValidationSummary(true, "Account creation was unsuccessful. " +
"Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>
This will work fine if you are using Code First with EF, but the class I am using for my data validation resides on the Web Service. I am using
Database First with EF.
What is happening is that the model is not reflecting the validation directives when the Submit button is pressed.
I'm trying to find out how I can use this middle tier class model for my validation. According to EF and DataBase First, you should be able to set up the validation this way.
Oh, it seems that you are first creating the databases in SQL, and then import these tables into EF (*.edmx) file... Am I right?
If yes, I think you can extend your model classes because the auto-generated model classes are partial classes. So you can do this like:
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(Product_MetaData))]
//Suppose this is the model class generated by EF.
public partial class Product
{
public Product()
{
this.ProductGuid = Guid.NewGuid();
this.AddTime = DateTime.Now;
}
public class Product_MetaData
{
public Guid ProductGuid { get; set; }
[Required(ErrorMessage = "Product cannot be null!")]
public string ProductName { get; set; }
public DateTime AddTime { get; set; }
}
}
I tried this approach, but I get a design time compile error with the Customer object in my AddCustomer method where the Customer class now cannot recognize any of the properties belonging to the customer object.
There are articles on the web how to do this with CodeFirst, but I have seen
none on how to do this with DataBaseFirst. How can this be accomplished?
If you are working with MVC, So have you checked whether you've got Html.Validation…… or something like this? Please show us your View page's aspx codes.
wsyeager36
Member
385 Points
458 Posts
DataAnnotations with EntityFramework (Database First) method
Aug 23, 2011 07:26 PM|LINK
I have a project in which I have a database model class provided along with a separate EDMX EF model. In the same solution, I have a web service which accesses this project along with the model class. I want the model class to perform data annotions against the front end for validation, but is not getting validated at all.
For brevity, the model class (in my Model project) is as follows. My web service references this class and is used as the interface.
When I hit the Submit button on the form, it tries to add the record and does not do any validation. A runtime error occurs informing me that the email address is required. I obvioulsy want this validation to be done up front with the data annotations.
How can I accomplish this?
Bill Yeager
MCP.Net, BCIP
wsyeager36
Member
385 Points
458 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Aug 24, 2011 01:58 AM|LINK
An actual runtime error is coming back saying
that the email address should not be NULL when the record tries to be added. This
is correct. The database column requires a value.
I thought that by having the data annotations in the model, if there is
something wrong with the front end and the model is not valid once the form
tries to be posted, the corresponding data annotation error should display on
the form. I was under the impression that there is no need for writing any
specific client side validation. The model is supposed to take care of that for
you. Am I incorrect in this assumption?
Bill Yeager
MCP.Net, BCIP
wsyeager36
Member
385 Points
458 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Aug 24, 2011 08:29 PM|LINK
Alan, I've already viewed that article. It is not set up for the Database First method. It is set up for Code First. The way I have my Model class set up is supposed to be the way to set it up using EntityFramewowrk with the DataBase First methodology. Take a look at the previous post to see how the class is set up.
Additionally, the content in there is outdated as far as the DLLs go. Check the DLLs I'm using in the above post.
The bottom line is this....
My data model for the Email field specifies the following:
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
The ModelState.Count parameter is correct and the ModelState.Keys are correct along with their respective values that I am checking during debugging in the collection. It's not setting the ModelState.IsValid property to FALSE based on the above directives.
The Model directives above are not being triggered on the front end for validation.
It's amazing how I can't get a simple direct answer of how to use DataAnnotations using the DataBase First methodology...
How can this be resolved?
Bill Yeager
MCP.Net, BCIP
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Aug 25, 2011 03:49 AM|LINK
Hello:)
Make sure that your view (aspx pages in the MVC) has used Validation technology, something looks like this:
e.g:
[PropertiesMustMatch("Password", "ConfirmPassword",
ErrorMessage = "The password and confirmation password do not match.")]
public class RegisterModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Email { get; set; }
[Required]
[ValidatePasswordLength]
public string Password { get; set; }
[Required]
public string ConfirmPassword { get; set; }
}
And your aspx codes should be:
<% using (Html.BeginForm()) { %>
<%= Html.ValidationSummary(true, "Account creation was unsuccessful. " +
"Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
<%= Html.LabelFor(m => m.UserName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(m => m.UserName) %>
<%= Html.ValidationMessageFor(m => m.UserName) %>
</div>
More at: http://msdn.microsoft.com/en-ca/magazine/ee336030.aspx
wsyeager36
Member
385 Points
458 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Aug 25, 2011 05:41 PM|LINK
This will work fine if you are using Code First with EF, but the class I am using for my data validation resides on the Web Service. I am using Database First with EF.
What is happening is that the model is not reflecting the validation directives when the Submit button is pressed.
I'm trying to find out how I can use this middle tier class model for my validation. According to EF and DataBase First, you should be able to set up the validation this way.
Bill Yeager
MCP.Net, BCIP
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Aug 26, 2011 12:50 AM|LINK
Oh, it seems that you are first creating the databases in SQL, and then import these tables into EF (*.edmx) file... Am I right?
If yes, I think you can extend your model classes because the auto-generated model classes are partial classes. So you can do this like:
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(Product_MetaData))]
//Suppose this is the model class generated by EF.
public partial class Product
{
public Product()
{
this.ProductGuid = Guid.NewGuid();
this.AddTime = DateTime.Now;
}
public class Product_MetaData
{
public Guid ProductGuid { get; set; }
[Required(ErrorMessage = "Product cannot be null!")]
public string ProductName { get; set; }
public DateTime AddTime { get; set; }
}
}
And in the controller's calling——
if (!ModelState.IsValid)
{
return View();
}
wsyeager36
Member
385 Points
458 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Sep 05, 2011 11:27 PM|LINK
I tried this approach, but I get a design time compile error with the Customer object in my AddCustomer method where the Customer class now cannot recognize any of the properties belonging to the customer object.
There are articles on the web how to do this with CodeFirst, but I have seen none on how to do this with DataBaseFirst. How can this be accomplished?
When I debug the "if (ModelState.IsValid)" in my client, the property always returns true. It's as if the DataAnnotations are not evenBill Yeager
MCP.Net, BCIP
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Sep 06, 2011 01:13 AM|LINK
Hello wsyearger36,
Do you mean even if you've added [Required], it still ignore it?
wsyeager36
Member
385 Points
458 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Sep 06, 2011 02:31 PM|LINK
Yes.....
Bill Yeager
MCP.Net, BCIP
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: DataAnnotations with EntityFramework (Database First) method
Sep 07, 2011 01:16 AM|LINK
Hello again,
If you are working with MVC, So have you checked whether you've got Html.Validation…… or something like this? Please show us your View page's aspx codes.
Thx again