【发布时间】:2018-10-31 10:59:49
【问题描述】:
我正在研究 ASP.NET Core,我发现了一个关于继承的事物(因为 IdentityUser 可以由 ApplicationUser 派生)。
这里只是一个演示来解释它是什么:
实体:
public class Animal
{
public string Name { get; set; }
}
public class Cat : Animal
{
// cat has no wings...
}
public class Eagle : Animal
{
public object LeftWing { get; set; }
public object RightWing { get; set; }
}
数据库上下文(仅用于演示):
public void SaveChanges(Animal animal)
{
// save changes for "Animals" table...
}
并使用它:
var cat = new Cat { Name = "Norris" };
var eagle = new Eagle { Name = "Voldemort", LeftWing = "good", RightWing = "broken" };
SaveChanges(cat);
SaveChanges(eagle);
// in asp.net core, this can be:
// public class ApplicationUser : IdentityUser {}
// _dbContext.Users.Add(new ApplicationUser { ExtensionProperty = "foo" });
// but we forgot to update the "Users" table
// which refers to "ApplicationUser" class instead if "IdentityUser" class by default
// _dbContext.Users.Add(new IdentityUser());
由于猫没有翅膀,我们不需要添加左翼和右翼作为两个属性。
另外,并不是所有的动物都有翅膀,所以不需要在表格Animals中定义。
为了解决这个问题,我们可以创建两个表Cats 和Eagles。
但在这篇文章中,我只想提到继承。
当儿子有2个翅膀而爸爸没有时,什么时候可以用儿子代替爸爸?
【问题讨论】:
-
您是在询问每个类型的表与每个层次结构的表吗?用关键字实体框架搜索那些,那里有足够的材料来回答你的问题(我认为)。如果不改写你的问题,因为你根本不清楚你在问什么(无论如何对我来说)。
-
@Igor 哦。对不起。我的意思是,当我尝试使用表中未定义的一些扩展属性更新数据库时。调试器不会对此发出任何警告。这仍然是一个有效的操作。
标签: c# inheritance visual-studio-code vscode-debugger