Mik*_*ike 3 c# entity-framework code-first ef-code-first
我已经设置了我的EF代码优先数据库,但想要添加其他派生属性.(是的,它应该在视图模型中,我们可以讨论另一种时间为什么这样.)我创建了一个扩展实际表类的部分类.如果我添加一个[NotMapped]
新的部分,它会避免映射我在那里添加的其他属性还是它将应用于整个类?
它适用于整个班级.请记住,部分类只是将类拆分为多个文件的一种方法.来自官方文档:
在编译时,合并部分类型定义的属性.
所以这:
[SomeAttribute]
partial class PartialEntity
{
public string Title { get; set; }
}
[AnotherAttribute]
partial class PartialEntity
{
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
相当于写作:
[SomeAttribute]
[AnotherAttribute]
partial class PartialEntity
{
public string Title { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果要添加部分类而不包含模型中包含的属性,则需要将NotMapped
属性添加到各个项目:
partial class PartialEntity
{
public string Title { get; set; }
}
partial class PartialEntity
{
[NotMapped]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)