what 's the use of creating a metadata class and a partial class for the model class c#

ado.net-entity-data-model c# entity-framework-6 model-view-controller

Accepted Answer

Every time you save the EDMX, the entity classes, the first class you display, is produced from the database (or when you execute the T4 Template).

Consequently, the file holdingpublic partial class MyClass underneath the EDMX to be revived. You cannot edit it since your modifications will be lost the moment someone adds or refreshes a table.

Entity classes are created as partials for this reason, allowing you to modify them in another partial of the same class.

The same property cannot be redefined in the other partial class if you wish to annotate the properties of your entity with information since a type may only use a given name once. As a result, you cannot:

// Entity class
public partial class FooEntity
{
    public string Name { get; set;} 
}

// User-created partial class
public partial class FooEntity
{
    [Required]
    public string Name { get; set;} 
}

Because such code indicates that you wish to identify two propertiesName in theFooEntity which is invalid, class.

Therefore, you'll need to find another method of including information in the type. Go into the[MetadataType] attribute. To do this, a new class is created and annotated with the same attributes. Here, the information is resolved based on member name via reflection.

As a result, when you create a metadata class for the aforementioned annotation:

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

It may be used with the user-created partial:

// User-created partial class
[MetadataType(typeof(FooEntityMetadata))]
public partial class FooEntity
{
}

Additionally, you may provide members that give the entity model new capabilities in the later subpart. New ([NotMapped] ) features and novel techniques, for instance.

7
12/16/2016 12:59:59 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow