b-abpvnet02

Volo.Abp.EntityFrameworkCore

Volo.Abp.EntityFrameworkCore.SqlServer

添加entityFrameWorkmodule

namespace MVC.EntityFrameWork
{
    [DependsOn(typeof(AbpEntityFrameworkCoreModule))]
    public class MVCEntityFrameWorkModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddAbpDbContext<MyMVCDbContext>();
            Configure<AbpDbContextOptions>(opt =>
            {
                opt.UseSqlServer();
            }
            );
        }
    }
}

添加abpdbContext

namespace MVC.EntityFrameWork
{
    [ConnectionStringName("Default")]
    public class MyMVCDbContext : AbpDbContext<MyMVCDbContext>
    {
        public MyMVCDbContext(DbContextOptions<MyMVCDbContext> options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}

在web层使用

[DependsOn(typeof(AbpAspNetCoreMvcModule),
        typeof(MVCEntityFrameWorkModule))]
    public class MyWebStartModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var services = context.Services;
            services.AddControllersWithViews();
        }
}

在controller使用,正常controller是调用应用层,而不是db层

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly MyMVCDbContext _myMVCDbContext;

        public HomeController(ILogger<HomeController> logger, MyMVCDbContext myMVCDbContext)
        {
            _logger = logger;
            this._myMVCDbContext = myMVCDbContext;
        }
}

 

posted @ 2021-12-06 23:50  vba是最好的语言  阅读(54)  评论(0)    收藏  举报