派生类用作实体框架中的模型
我在另一个项目中有一个课程,并希望将该课程扩展到具有实体框架的另一个项目。这可能吗?它会生成带有基类属性的表吗?
示例:
iuser.cs
(来自没有实体框架的项目)
public interface IUser
{
string Username { get; init; }
string Password { get; init; }
}
user.cs
(来自没有实体框架的项目)
public class User : IUser
{
string Username { get; init; }
string Password { get; init; }
}
userdb.cs
(来自带有实体框架的项目)
public class UserDB : User
{
public int Id { get; set; }
}
I have a class in a different project and want to extend that class to another project with Entity Framework. Is this possible? Will it generate a table with the properties from the base class?
Example:
IUser.cs
(from project with no Entity Framework)
public interface IUser
{
string Username { get; init; }
string Password { get; init; }
}
User.cs
(from project with no Entity Framework)
public class User : IUser
{
string Username { get; init; }
string Password { get; init; }
}
UserDB.cs
(from project with Entity Framework)
public class UserDB : User
{
public int Id { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在EF核心中,这会自动起作用。在for Legacy .NET项目中,您必须映射
OnModeLcreating()
之类的继承属性:否则,数据库表将不会存储
user
class的属性In EF Core this works automatically. In EF for legacy .NET projects you have to map the inherited properties inside of
OnModelCreating()
like this:Otherwise, the database table will not store the properties of the
User
class是的,EF会将
UserDB
视为具有属性的类:ID,用户名和密码。Yes, EF would see
UserDB
as a class that had the properties: Id, Username and Password.