我正在 ASP.NET 核心 MVC 中构建网络销售平台,构建 DbContext 的最佳方式是什么?

I'm building a web sales platform in ASP.NET Core MVC, what's the best way to structure the DbContext(s)?

我有:

  1. 常客
  2. 商家
  3. 当然还有我们的管理员和内部团队

我是否将所有用户帐户存储在 [regular visitors] 数据库中,然后将“敏感”数据分离到 [merchants][internal] 数据库中以至少确保某种安全性或有更好的选择吗?

此外,我正在使用 Entity Framework 来构建数据库 - 虽然我目前只在 Web 界面上工作,但我最终想添加一个移动应用程序界面,可能还有一个 windows项目的应用程序接口 - 如何确保共享模型,这样我就没有多余的代码可以复制?

真诚感谢任何帮助。

谢谢。

*目前将所有数据存储在单一数据库中。

Let's clarify few things before starting, if you could observe now a days, all typical modern application handle request either from mobile device or web browser (all the platforms like Windows, Mac, Linux). and from the desktop platform. See the drawing below:

Application Architecture:

因此,请求来自不同的设备,我们称之为(跨平台),但我们可以使端点通用。那里有不同的技术。你可以考虑Asp.net Web API

Now come to your point.

"How do I ensure the models are shared so I don't have redundant code to duplicate?"

There are couple of way to restrict code redundancy. You can consider SOLID principle. Within the SOLID you could specifically implement Single Responsibility means a class should only have one responsibility additionally you could implement Inheritance of OOP so that you can reuse of the base code in your derive class when your project/application would continuously extended.

您可以在网上找到有关上述条款的大量示例。

So what I am trying to explain is make your backend application universal so that it can handle all the request no matter where they are coming from. So that it would be easier for you to handle code reusability and eventually it would restrict the redundancy as well.

Now Consider Your Domain Model:

Into your ApplicationDbContext you can now define all of your domain model like you can think of your business model as the domain model for example: RegularVisitors, Merchants and admins and internal team as Users domain model, You can design your ApplicationDbContext as below:

public class ApplicationDbContext: DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<Merchants> Merchants{ get; set; }
        public DbSet<RegularVisitors> RegularVisitors{ get; set; }
        public DbSet<User> User { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           
            modelBuilder.Entity<Merchants>().ToTable("Merchants");
            modelBuilder.Entity<RegularVisitors>().ToTable("RegularVisitors");
            modelBuilder.Entity<User>().ToTable("User");
        }
       
    }

This is how you could move forward considering your requirement. For details implementation you could consider to implement your requirement accordingly

希望以上步骤和实施能相应地指导您。