Reputation: 942
I have set up my EF code-first database but want to add additional derived properties. (Yes, it should be in a view model, we can discuss another time why it is this way.) I have created a partial class extending the actual table class. If I add a [NotMapped]
to the new partial, will it avoid mapping the additional properties I add there or will it apply to the entire class?
Upvotes: 3
Views: 1677
Reputation: 118937
It will apply to the entire class. Remember that a partial class is simply a way of splitting a class into multiple files. From the official docs:
At compile time, attributes of partial-type definitions are merged.
So this:
[SomeAttribute]
partial class PartialEntity
{
public string Title { get; set; }
}
[AnotherAttribute]
partial class PartialEntity
{
public string Name { get; set; }
}
Is equivalent to writing:
[SomeAttribute]
[AnotherAttribute]
partial class PartialEntity
{
public string Title { get; set; }
public string Name { get; set; }
}
If you want to add a partial class without having the properties included in the model, you will need to add the NotMapped
attribute to the individual items:
partial class PartialEntity
{
public string Title { get; set; }
}
partial class PartialEntity
{
[NotMapped]
public string Name { get; set; }
}
Upvotes: 6