Does [NotMapped] add a whole class display exception to a partial class?

I created my first EF database, but I want to add additional derived properties. (Yes, this should be in the presentation model, we can discuss another time why this is so.) I created a partial class that extends the actual class of the table. If I add [NotMapped]to the new partial, will it not allow me to display additional properties that I add there, or is it applicable to the whole class?

+4
source share
1 answer

It applies to the whole class. Remember that a partial class is just a way to split a class into multiple files. From official white papers :

At compile time, the attributes of the partial type definitions are combined.

So this is:

[SomeAttribute]
partial class PartialEntity
{
    public string Title { get; set; }
}

[AnotherAttribute]
partial class PartialEntity 
{
    public string Name { get; set; }
}

It is equivalent to writing:

[SomeAttribute]
[AnotherAttribute]
partial class PartialEntity
{
    public string Title { get; set; }
    public string Name { get; set; }
}

If you want to add an incomplete class without the properties included in the model, you will need to add the attribute NotMappedto the individual elements:

partial class PartialEntity
{
    public string Title { get; set; }
}

partial class PartialEntity 
{
    [NotMapped]
    public string Name { get; set; }
}
+6
source

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


All Articles