繁体   English   中英

ASP.NET MVC 5 Web应用程序中是否应该只有一个DbContext和数据库才能将用户帐户与对象相关联?

[英]Should there be single DbContext and Database within the ASP.NET MVC 5 Web Application to be able associate User's Accounts with objects?

我正在创建Web应用程序。 我创建了一个class Person和一个class Meeting有时Person与会议相关联。 所以我做了:

public class PersonContext : DbContext {

        public PersonContext(): base("PersonContext") { }

        public DbSet<Person> Persons { get; set; }
        public DbSet<Meeting> Meetings { get; set; }
        public DbSet<Status> Statuses { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

正如我们看到的那样,它与以下内容建立了连接: <add name="PersonContext" connectionString="Data Source=(LocalDb)\\v11.0;Initial Catalog=WebApplication2;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> <add name="PersonContext" connectionString="Data Source=(LocalDb)\\v11.0;Initial Catalog=WebApplication2;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

一切都很好, 直到我发现我也需要将用户帐户与Meeting关联。

但是用户帐户保存在其他数据库中

  public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {

        }

        public static ApplicationDbContext Create()
        {public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            return new ApplicationDbContext();
        }
    }

它是连接字符串:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\\v11.0;AttachDbFilename=|DataDirectory|\\aspnet-WebApplication2-20140711041006.mdf;Initial Catalog=aspnet-WebApplication2-20140711041006;Integrated Security=True" providerName="System.Data.SqlClient" />

问题:我应该怎么做才能将Meeting与用户帐户相关联。 我应该改变:

public ApplicationDbContext()
                : base("DefaultConnection", throwIfV1Schema: false)

到公共ApplicationDbContext():base(“ PersonContext”,throwIfV1Schema:false)

或删除class PersonContext并将其所有字段移至ApplicationDbContext

我已经做到了:

 public class PersonContext : ApplicationDbContext {

        public PersonContext() { }

        public DbSet<Person> Persons { get; set; }
        public DbSet<Meeting> Meetings { get; set; }
        public DbSet<Status> Statuses { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

在更新数据库后,我得到:

One or more validation errors were detected during model generation:

    WebApplication2.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
    WebApplication2.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
    IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
    IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

您应该使用自己的类扩展生成的ApplicationDBContext (或者您自己的DBContext应该继承自ApplicationDBContext ),以便所有内容都保存在同一数据库中,并且可以通过同一DB Context访问。

不要创建另一个单独的上下文。 这将使您所描述的那种关系很难实现。

如果您选择在派生上下文中重写OnModelCreating() ,则必须调用基础OnModelCreating() ,因为它是IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim>.OnModelCreating()方法来设置所有标识类的映射。

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
  base.OnModelCreating(modelBuilder);
  modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

我将其发布为答案,但是如果没有Anders Abel的指示,这将很难做到。

所以我删除了PersonContextPersonInitilizer 来自PersonContext字段移至IdentityModels.cs以创建ApplicationDbContext类。

种子方法的内容已移至Configuration.Migrations.Seed(...)方法。 同样,所有对PersonContext引用都更改为ApplicationDbContext

然后我做了: base.OnModelCreating(modelBuilder); 在OnModelCreating中

我还从Web.config删除了整个块:

 <contexts>
     <context type="WebApplication2.Models.ApplicationDbContext, WebApplication2">
        <databaseInitializer type="WebApplication2.Migrations.Configuration, WebApplication2" />
      </context>
    </contexts>

之后,您应该打开“程序包管理器”并键入并执行: update-database并且应该没问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM