我已经设置了我的EF代码优先数据库,但想要添加其他派生属性。 (是的,它应该在视图模型中,我们可以讨论另一个时间为什么这样。)我创建了一个扩展实际表类的部分类。 如果我将[NotMapped]添加到新的部分,它会避免映射我在那里添加的其他属性还是它将应用于整个类?
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?
最满意答案
它适用于整个班级。 请记住,部分类只是将类拆分为多个文件的一种方法。 来自官方文档 :
在编译时,合并部分类型定义的属性。
所以这:
[SomeAttribute] partial class PartialEntity { public string Title { get; set; } } [AnotherAttribute] partial class PartialEntity { public string Name { get; set; } }相当于写作:
[SomeAttribute] [AnotherAttribute] partial class PartialEntity { public string Title { get; set; } public string Name { get; set; } }如果要在不具有模型中包含的属性的情况下添加NotMapped类,则需要将NotMapped属性添加到各个项:
partial class PartialEntity { public string Title { get; set; } } partial class PartialEntity { [NotMapped] public string Name { get; set; } }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; } }更多推荐
发布评论