How to validate Minlength and MaxLength uisng Fluent Api in Asp.Net Core 3.0 Code first - ef-code-first

I have Field named Title.
Then How to set MinLength is 10 and MaxLength 50 using fluent api?

You can't validate with fluent api. It's just for database modeling. You need to use Data Annotations or Fluent Validation. I recommend you to use Fluent Validation with Form Helper to validate your models.

Related

ASP.Net Core and model validation

Suppose I have a model which I need to validate. I can add some validate attributes to the properties I want to validate. And it works pretty fine. But at some point I want to validate this model depending on other models (I will need t query db). And here there are some options.
create a special validate attribute which will lack dependency injection
delegate this validation to some business layer (manager) and in controller
_
if (Model.IsValid) {
if(!await Manager.Create(myModel)) {
Model.CopyErrors(Manager.Errors); // Extension method for Model
}
}
So what is the correct way of handling such a situation?
P.S. I am using asp.net core and entity framework core
What you need is Fluent Validation

Asp.net MVC 5 model validation regex not validating special characters

I have using ASP.NET MVC 5 and for validation I am using the regular Jquery unobtrusive validation library.
Currently I am facing an issue, The regular expression validation is working all right on client side but on server side its not validating the value.
[RegularExpression(#"^[0-9]{19}$", ErrorMessage = "Invalid Value")]
public string Value{ get; set; }
this is the simple regular expression to validate 19 digits. Its working fine on client side it'll now allow ' or - or any thing to pass on but if i submit directly to server than the model is not being validated.
Do I need to do some thing special?
ModelBinding ( the process of mapping post/get values to a complex model) occurs when you send data via post/get/put/delete requests, if you are manually creating a class with validation classes somewhere those data/validation annotations won't apply.
you can validate models manually - Validate list of models programmatically in ASP.NET MVC
That said, there is a library called PostSharp that can enforce those rules even when you are creating custom object/models outside of MVC ( CodeContracts ) and if it doesn't validate it throws an exception. Keep in mind that this library is paid but it has free version available that might fit your needs.

Domain logic in a Entity framework Attribute

I've an ASP.NET MVC project with a domain and a data layer (contains just EF and migrations).
Now, I have to validate a Client's VATNumber field with a specific formula I'm having doubts where to place it.
I would like to use the Controller's ModelState to check if the field is valid, using a EF DataAnnotation ValidationAttribute, so I don't have to validate it in the Controller's Saving Method. But I'm conflicted having to put business logic in the data layer.
Is there some kind of alternative to ValidationAttribute, so it can be "injected"? Or I'm just looking at this wrongly?
Validation attributes is not part of Entity Framework.
If you want splite data layer from presentation layer, you can use Viewmodels and bind them on datamodels with Automapper. In this case you place domain validation on viewmodel and validation of datalayer on datamodel.
In very complicated cases you may have three models: viewmodel, domainmodel and datamodel, but in common cases viewmodel and datamodel is enough.

ASP.NET MVC3 Generate View Model including validation attributes

I am wondering if there is a way to generate a View Model from database table including validation (required,length,datatype). Of course I will edit it and enhance it.Otherwise it is a waste as I have to type in everything
We are implementing a "Dynamic" approach to validation in one of our projects where the validation rules are encoded in a database table and we override GetValidators() in a custom implementation of DataAnnotationsModelValidatorProvider to inject custom and/or standard validation attributes. These can then be propagated to the client if we want them to. As for creating a viewModel from the existing database, you can use t4 files to read the db schema and generate classes for you and inject validation attributes on the model as it is being created.

Do standard ASP.NET controls connected to an entity datasource provide built in validation?

The entity framework connects to my database to retrieve columns, datatypes, relations, etc. It also knows which columns can be null and not null.
If I connect a regular asp.net grid to an entity datasource, it can generate the grid automatically based on the entity. It knows which fields should be a checkbox based on the datatype, etc.
Since data types are built into the entity class, can a regular asp.net control (like a grid or formview) also perform validation automatically? (or generate the necessary validation controls at least?)
Thanks,
Kevin
Yes, you can display validation errors including validation types using asp:ValidationSummary control. Here one project with this approach used on gridview: http://code.msdn.microsoft.com/ASPNET-Web-Forms-97f8ee9a , check out editing students.
The way to add more validation rules to entities is by attaching meta data.
If you are using EF Code First you can apply rules directly else by adding meta data class, here example:
[MetadataType(typeof(EntityNameMetaData))]
public partial class EntityName {} // name of entity which want to add validation
public class EntityNameMetaData // this is a place, where put validation rules
{
[StringLength(25, ErrorMessage = "First name must be 25 characters or less in length.")]
[Required(ErrorMessage = "First name is required.")]]
//custom or other validation rules
public String EntityProperty
}
this is not possible automatically in asp.net, you would have to define the columns and create item templates to implement this.

Resources