Code first: does the free api work?

I am reading a book written by Julie Lerman on Code First. According to the book, annotations and fluent api give the same result. It all depends on the style of the developer.

I know that annotations let you customize how code first generates database objects, and how MVC customizes user interface elements. Let's say I use [Required, MaxLength (50)]. The attribute will generate NOT NULL, nvarchar (50) in the database. It will also check the input for this field.

[Required, MaxLength(50)] public string Name { get; set; } 

What if I decide to use the Fluent API for the first code setup. I still need annotations to affect user interface elements or use a free API, will that be enough?

EDIT

What about annotations like Display, which are for UI purposes only? Do they have equivalents? If not, do I need to use annotations?

 [Display(Name = "Date of Birth")] public DateTime BirthDate { get; set; } 

thanks for the help

+6
source share
3 answers

Data annotations are the easiest way to tell a class to enforce some validation rule. You can do the same with the Fluent API. Some people like to do this with data annotations, and some people like it with a free API.

Reasons to like it with data annotations

1) Store the information about the confirmation of my essence in one place along with the definition of the essence

Reasons for Using Fluent API This Way

1) Keep my essence clean. He will have only my property information. There is no verification information. Clean and simple POCO. I will write validation in the OnModelCreating method in my data context class.

You cannot perform all Fluent API actions using the Data Annotations method. just like you have not a few attributes of data attributes that are not equivalent with the Fluent API (Ex: HasMinLength). HasMinLength is what we will do to test our model, which usually makes sense in the user interface.

You cannot use the Fluent API only to test the user interface model. The main role of the Fluent API is to look at the current configuration that we are writing and act when creating a model (database) from entities. Remember that we override the OnModelCreating method to write our current API configuration . Thus, to test the user interface (of my ViewModel), I would use the DataAnnotation method and use the free API if I want to define some thing related to my datamodel, for example Define foreign key or Map this Entity, in a table with a different name etc.

EDIT: According to the editing question

In this case, you should use data annotations. If you make the code first. You may remember that this object will be your database table (of course, you can tell EF to ignore / rename certain columns). In this case, I would keep Entities clean and Create a ViewModel , which I will use in my user interface. I will add DataAnnotations to my ViewModel to handle it. I can write mapping code that displays data from the ViewModel in Model and Model in ViewModel where necessary.

+9
source

If the entity model classes are doubled as your viewmodel classes and you use the default value from the DataAnnotationsValidationProvider field, you will need the dataannotations attributes for the model properties to get the confirmation.

However, you should not double your entity classes as viewmodel classes. Take, for example, a controller that must have the ReturnUrl property in its model. You would not want this in your model / database. Due to the differences between the View model and Entity model, 2 must be truly separate (but cohesive) layers in your application. You can make them connected using a library such as AutoMapper.

This is one of the reasons I prefer the free API. If you adhere to a free API, then never put any attributes on any classes or properties of an entity model. When it comes time to show, insert, or update data, you only place attributes in the viewmodel classes.

In addition, the [Required] attribute for an entity type performs validation during SaveChanges, while the [Required] attribute in the view model performs validation during model binding.

+4
source

According to Julia Lerman's book on DbContext, you do not need additional annotations to your Fluent API configuration. The Name property will be validated by the validation API, as if it were configured with data annotations.

According to the same book, MaxLength and Required are the only validation attributes with smooth APIs.

+1
source

Source: https://habr.com/ru/post/916397/


All Articles