适用于 Asp.Net MVC 5 应用程序中所有模型的 Models.ApplicationDbContext? [英] Models.ApplicationDbContext for all models in an Asp.Net MVC 5 application?

查看:34
本文介绍了适用于 Asp.Net MVC 5 应用程序中所有模型的 Models.ApplicationDbContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Asp.Net MVC 5 网站.我需要在 ApplicationUser 中添加自定义字段并将其与其他模型关联(添加外键).我想我应该只使用一种上下文类型.但是,代码脚手架已经生成了以下 ApplicationDbContext 类.我可以把我所有的 public DbSet<...>... { 得到;放;} 在课堂上?或者有更好的模式吗?

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>
    {
    }
}

推荐答案

有一个很好的视频解释了这个问题.只需查看 Scott Allen 的免费 ASP.NET MVC 5 Fundamentals 课程即可.确切的 答案在这里(从 3:30 开始).

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).

这篇关于适用于 Asp.Net MVC 5 应用程序中所有模型的 Models.ApplicationDbContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆