派生类用作实体框架中的模型

发布于 2025-02-10 02:51:12 字数 550 浏览 5 评论 0原文

我在另一个项目中有一个课程,并希望将该课程扩展到具有实体框架的另一个项目。这可能吗?它会生成带有基类属性的表吗?

示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2025-02-17 02:51:12

在EF核心中,这会自动起作用。在for Legacy .NET项目中,您必须映射OnModeLcreating()之类的继承属性:

            var mapping = modelBuilder.Entity<UserDB>();
            mapping.Map(m =>
            {
                m.MapInheritedProperties();
            });

否则,数据库表将不会存储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:

            var mapping = modelBuilder.Entity<UserDB>();
            mapping.Map(m =>
            {
                m.MapInheritedProperties();
            });

Otherwise, the database table will not store the properties of the User class

空‖城人不在 2025-02-17 02:51:12

是的,EF会将UserDB视为具有属性的类:ID,用户名和密码。

Yes, EF would see UserDB as a class that had the properties: Id, Username and Password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文