1

我有一個看起來像這樣的模型類:我可以在不擴展表的情況下擴展模型嗎?

public class Foo 
{ 
    [Key] public int Id { get; set; } 
} 

別的地方在我的代碼我加了私人子類:

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)); 

我沒有想到實體框架會發現有一個Bar子類,sinc e它是私人嵌套在控制器中的,除Models命名空間之外。我可以停止EF修改表嗎?

我試圖[NotMapped]忽略Name屬性,但EF仍然添加一個Discriminator列,因爲它使用的繼承策略。

+1

您是否嘗試在「Bar」類中添加該屬性 – octavioccl

回答

3

您可以在整個班級中使用[NotMapped]屬性。

這是該屬性的定義:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false)] 
public class NotMappedAttribute : Attribute 
{ 
} 

AttributeUsage說,你可以用它在類,而不僅僅是財產。

相關問題