Does it make sense to use MetadataType to force validation in the case of Code First?

I seem to understand the reason for helping MetadataTypeAttribute Add validation to the model in the case of Database First , because we want to avoid the changes that will be overwritten the next time the model is generated from the database next time.

I noticed that some people who define validation use MetadataType even when they use Code First , and there is no chance of their class entities being replaced by some kind of automatic code generation.

It makes no sense not to apply these DataAnnotations directly to the real Entity class and instead separate them from the partial class definitions and then reference MetadataType , even when using Code First to define the Entity model?

 public class MyEntity { [Required] public string Name { get; set;} } 

against

 public partial class MyEntity { public string Name { get; set;} } [MetadataType(typeof(MyEntityMetadata))] public partial class MyEntity { } public class MyEntityMetadata { [Required] public string Name { get; set;} } 
+7
c # validation asp.net-mvc entity-framework
source share
4 answers

Does it make sense to use these DataAnnotations directly in the actual Entity class and instead separate them into partial class definitions and then reference MetadataType even if you use the Code First approach to define the Entity Model?

In most cases, this does not make sense, because it involves unnecessary code duplication in order to associate some attributes with properties.

It makes no sense if you create an entity class model with code.

It also makes no sense if it is created using a specific user code that you control (for example, the T4 template), because you can customize your own generation.

The only case where this makes sense is when you do not have control over the code of an entity class (for example, a class coming from a third-party library). In this case, you can use the AssociatedMetadataTypeTypeDescriptionProvider class to map metadata to a third-party class.

For example, let's say the following class comes from another library without source code:

 public sealed class ExternalEntity { public string Name { get; set;} } 

Then you can define a metadata class:

 public class ExternalEntityMetadata { [Required] public string Name { get; set;} } 

and associate it with ExternalEntity using the TypeDescriptor.AddProvider once (at application startup time or something else):

 TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider( typeof(ExternalEntity), typeof(ExternalEntityMetadata), typeof(ExternalEntity)); 
+3
source share

I don’t know why you are trying to use the first database technique for a more complete, say, code first, since you can create ViewModels to achieve your goal. In addition, not all data annotations are supported in the Entity Framework.

MetaDataType Limitations

  • It cannot be applied to a property and can only be applied to one class for each type of class.
  • This attribute cannot be inherited, so you cannot configure it.
  • On the other hand, this attribute can be applied to partial classes, which is the main purpose of this attribute.
  • This attribute will be respected by ASP.NET MVC, but will not be read by the Entity Framework.

Disadvantages of Using MetaDataType

  • you need to use ViewBag , ViewData or something else to go through additional information to the view
  • Your design is less tested because it relies on a static object mechanism.
  • It is also not required, and someone can omit it without violating anything.
  • It also means that you break the model class into 3 files. One generated, one of yours and one with attributes.

If you want to add attributes to existing properties in the class (partially):

This may work or be ignored by EF, test it:

 public partial class YourModelClass { public string YourProperty{get;set;} } //Your Partial Class [MetadataType(typeof(YourModelClassMetaData))] public partial class YourModelClass { } //The class that adds your attributes public class YourModelClassMetaData { [Required] public object YourProperty{get;set;} } 
0
source share

It actually makes sense to create a class and use it many times. In the first code approach, you need data validation that can be implemented using data annotations, and when you have a lot of props with the same features, it will be easier to make your life easier. This is not just a rewrite, in which case it has other reasons. Hope to understand your question well, and my answer is appropriate.

0
source share

I think the questions are where the difference between the annotations of the model and code data comes first.

So first you have data validation

this is the attribute setting in your first code model and it sets up the database column configuration, and it sets the size and constraints for your data model. (This once populated usually does not change without data transfer.)

Model check

Model validation is your model with which you link your form. This model will contain more information for your user interface.

0
source share

All Articles