将[NotMapped]添加到分部类是否可以避免映射整个类?(Does adding [NotMapped] to a partial class avoid mapping the entire c

编程入门 行业动态 更新时间:2024-10-22 09:28:02
将[NotMapped]添加到分部类是否可以避免映射整个类?(Does adding [NotMapped] to a partial class avoid mapping the entire class?)

我已经设置了我的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; } }

更多推荐

本文发布于:2023-08-02 20:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部类   partial   adding   NotMapped   mapping

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!