我有一个看起来像这样的模型类:
public class Foo
{
[Key] public int Id { get; set; }
}
我在代码的其他地方添加了一个private 子类:
private class Bar : Foo
{
public string Name { get; set; }
}
现在,当我构建一个迁移时,我得到了这个:
AddColumn("dbo.Foo", "Name", c => c.String());
AddColumn("dbo.Foo", "Discriminator", c => c.String(nullable: false, maxLength: 128));
我不认为 Entity Framework 会发现 Bar
子类,因为它私密地嵌套在 Models
命名空间之外的 Controller 中。我可以阻止 EF 修改表吗?
我试过 [NotMapped]
忽略 Name
属性,但 EF 仍然添加了一个 Discriminator
列,因为它使用了继承策略。
最佳答案
您可以只对整个类使用 [NotMapped]
属性。
这是这个属性的定义:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false)]
public class NotMappedAttribute : Attribute
{
}
AttributeUsage
表示您可以在类上使用它,而不仅仅是在属性上。
关于c# - 我可以在不扩展表格的情况下扩展模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35659933/