Models.ApplicationDbContext for all models in an A

2020-02-07 18:02发布

问题:

I've creating an Asp.Net MVC 5 website. I will need to add customized fields in ApplicationUser and associate (add foreign keys) it with other models. I think I should just use one context type. However, the code scaffold already generate the following ApplicationDbContext class. Can I just put all my public DbSet<...> ... { get; set; } in the class? Or is there a better pattern?

namespace MyApp.Models
{
    // You can add profile data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : User
    {  
    }

    public class ApplicationDbContext : IdentityDbContextWithCustomUser<ApplicationUser>
    {
    }
}

回答1:

There is an excellent video explaining that matter. Just check the free ASP.NET MVC 5 Fundamentals course by Scott Allen. The exact answer is here (starts at 3:30).



回答2:

I would advise keeping them separate. There is really no reason to couple two parts of the system together. To add another DbContext just add a file to models called YourContext.cs.

public class YourContext: DbContext
{
    public YourContext() : base("name=YourContext")
    {
    }

    // Add a DbSet for each one of your Entities
    public DbSet<Room> Rooms { get; set; }
    public DbSet<Meal> Meals { get; set; }
}

Then in the root web.config

<add name="YourContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=YourContext; Integrated Security=True"" providerName="System.Data.SqlClient" />

When you run enable-migrations in the package manager console you will be asked which dbcontext you want to migrate. Pick YourContext.

EDIT: No need to add repos / unit of work the Entity Framework does this for you.



回答3:

Please note: This was written as in beta2 where ALLOT has changed! Hopefully most of it will stick but there are no guarantees until RC.

DO NOT USE NuGET package manger (until RC) as it does NOT pick-up on the .NET 5 packages required and it will install EF 6 and mess up your project. (We are after EF 7)

In the projects.json you need to have the following dependencies. (or beta2 when its out, or the latest on RC)

    "EntityFramework": "7.0.0-beta1",
    "EntityFramework.Relational": "7.0.0-beta1",
    "EntityFramework.Commands": "7.0.0-beta1",
    "EntityFramework.Migrations": "7.0.0-beta1",
    "EntityFramework.SqlServer": "7.0.0-beta1"

Add a new folder DBContexts and add a c sharp file with your new context stuff.

  public class BlaBlaDB : DbContext
    {
        public DbSet<Models.MyOtherModel> MyOtherModels { get; set; }

        protected override void OnConfiguring(DbContextOptions options)
        {
            options.UseSqlServer();
        }
    }

and in your config.json make sure to add a connection string, the exact same as the IdentityDB just with you new name. Then in startup.json register your databse.

 services.AddEntityFramework(Configuration)
                .AddSqlServer()
                .AddDbContext<DataContexts.IdentityDB>()
                .AddDbContext<DataContexts.BlaBlaDB>();

This has to compile because k will run this project and use the startup to inject your context and then execute everything you need. As of now VS2015 Beta does NOT have all/ or they do not work, the command for EF.

You need to go and install KRE for Windows.

Open command prompt, browse to your project directory, enter the solution and enter the following commands.

k ef context list
k ef migration add -c (context.from.above) initial
k ef migration apply -c (context.from.above)

You now have multi context migration. Just keep on adding context and repeat this as you need it. I used this on localdb, as the default project set-up so that it can work stand alone in any environment, like Linux.

Please Note: You still need to create a Service, containing the Interface and Implementation and then register that in startup.json More information here