【问题标题】:MVC5 VB.Net - Single dbContext and MigrationsMVC5 VB.Net - 单个 dbContext 和迁移
【发布时间】:2014-03-27 17:18:13
【问题描述】:

这在某种程度上与最佳实践有关,但也与一些实际问题有关。

我是 MVC 和 .Net 的新手,以前是 PHP 开发人员。总的来说我很喜欢它,Entity Framework / Code First 有点特别。

所以我在我的第一个项目上做了一些工作,我的第一个问题是似乎没有很多关于 VB 的文档 - 我不能使用 C#,因为这里的其他开发人员都是VB伙计们。我可以从整体上解决这个问题,但有没有人知道 MVC5 和 VB 的好资源、书籍或在线资源?

现在解决我的问题。我的项目将包含大量数据。我对创建模型和使用迁移来启动和运行表没有任何问题,我可以使用复杂的连接查询它们,让我的视图工作,动态表单等等。

目前我有两个数据连接,一个用于身份信息,一个用于其他所有内容(如 MVC 网站上的示例)。

本周概要发生了变化,我现在需要包含更多用户数据,并将我的用户链接到我的其他连接中的其他表。

例如,我需要添加一个 clientCompany 表,在我的 Users 表中具有相应的字段。给定的 clientCompany 将有 salesRegions,每个都有 postCodes 等。

那么要将我的 Identity User 表加入我的其他人,并设置外键,他们是否需要占用相同的连接和 dbContext?

我似乎一次只能为我的一个 dbContext 设置迁移,这让我觉得我需要将所有内容都放在一个 dbContext 中。我已经看到有关在我自己的 dbContext 类中继承 IdentityDbContext 的讨论,但我无法让它工作 - 只有身份表和由 FK 链接到它们的表正在创建/更新。

这是我的 IdentityModels.vb

Public Class ApplicationUser
    Inherits IdentityUser
    Property emailAddress() As String
    Overridable Property clientCompany() As ClientCompany
End Class

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)

    Public Class IdentityDbContext
        Public Property IdentityDbContext() As ApplicationDbContext
    End Class
    Public Sub New()
        MyBase.New("ExportEntities")
    End Sub
End Class

这是我自己的 dbContext 类,ExportEntities.vb

Namespace models

Public Class ExportEntities

    'Inherits DbContext
    Inherits IdentityDbContext

    'Metadata Heirarchy
    Public Property Products() As DbSet(Of Product)
    Public Property ProductVersions() As DbSet(Of ProductVersion)
    Public Property dbSplits() As DbSet(Of dbSplit)
    Public Property Sections() As DbSet(Of Section)
    Public Property FieldGroups() As DbSet(Of FieldGroup) 'Not all fields are organised into groups
    Public Property FieldNames() As DbSet(Of FieldName)

etc...

最后是我的连接字符串:

<connectionStrings>

    <add name="ExportEntities" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=&quot;|DataDirectory|\Export.mdf&quot;;Initial Catalog=&quot;Export&quot;;Integrated Security=True"
  providerName="System.Data.SqlClient" />
</connectionStrings>

谁能指点我正确的方向?

提前致谢

西蒙

【问题讨论】:

    标签: asp.net vb.net entity-framework asp.net-mvc-5


    【解决方案1】:

    最后我想通了,我会在这里发布基础知识,以防其他人受益。

    在我自己的数据类中,我继承了 DbContext 并为 Identity 表添加了 DbSets,并在末尾添加了 onModelCreating 子:

    Public Class MyDataClass
    
        Inherits DbContext
    
        'Identity Tables
        Public Property AspNetRoles As DbSet(Of IdentityRole)
        Public Property AspNetUserClaims As DbSet(Of IdentityUserClaim)
        Public Property AspNetUserLogins As DbSet(Of IdentityUserLogin)
        Public Property AspNetUserRoles As DbSet(Of IdentityUserRole)
        Public Property AspNetUsers As DbSet(Of ApplicationUser)
    
        'All of my other data...
    
        Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
            modelBuilder.Entity(Of IdentityRole)().HasKey(Of String)(Function(r) r.Id).[Property](Function(p) p.Name).IsRequired()
            modelBuilder.Entity(Of IdentityUserRole)().HasKey(Function(r) New With { _
                r.RoleId, _
                r.UserId _
            })
            modelBuilder.Entity(Of IdentityUserLogin)().HasKey(Function(u) New With { _
                u.UserId, _
                u.LoginProvider, _
                u.ProviderKey _
            })
        End Sub
    End Class
    

    在 IdentityModels.vb 中,我删除了将基础定义为 DefaultConnection 的部分,留下以下内容:

    Imports System
    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports exportApp.exportApp.models
    Imports System.Data.Entity
    Imports System.ComponentModel.DataAnnotations
    Imports System.ComponentModel.DataAnnotations.Schema
    
    ' You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    Public Class ApplicationUser
        Inherits IdentityUser
        Property emailAddress() As String
        Overridable Property clientCompany() As ClientCompany
    End Class
    

    我创建了一个类 ApplicationDbContext。注意MyDataClass 中的New() 子:

    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports System.Data.Entity
    
    Namespace exportApp.models
    Public Class ApplicationDbContext
        Inherits DbContext
    
        Public Overridable Property Users() As IDbSet(Of ApplicationUser)
            Get
                Return m_Users
            End Get
            Set(value As IDbSet(Of ApplicationUser))
                m_Users = value
            End Set
        End Property
    
        Private m_Users As IDbSet(Of ApplicationUser)
    
        Public Sub New()
            MyBase.New("MyDataClass")
        End Sub
    
        Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
            If modelBuilder Is Nothing Then
                Throw New ArgumentNullException("modelBuilder")
            End If
             modelBuilder.Configurations.AddFromAssembly(GetType(models.Configurations.ApplicationUserConfiguration).Assembly)
        End Sub
    End Class
    End Namespace
    

    最后,我在“Models”文件夹中添加了一个“Configurations”文件夹,并添加了以下五个类。这是我之前缺少的部分。

    ApplicationUserConfiguration.vb:

    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports System.Collections.Generic
    Imports System.Data.Entity.ModelConfiguration
    Imports System.Linq
    Imports System.Web
    
    Namespace myApp.models.Configurations
    Public Class ApplicationUserConfiguration
        Inherits EntityTypeConfiguration(Of ApplicationUser)
        Public Sub New()
            [Property](Function(u) u.UserName).IsRequired()
            HasMany(Of IdentityUserRole)(Function(u) u.Roles)
        End Sub
    End Class
    End Namespace
    

    IdentityRoleConfiguration.vb

    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports System.Collections.Generic
    Imports System.Data.Entity.ModelConfiguration
    Imports System.Linq
    Imports System.Web
    
    Namespace myApp.models.Configurations
    Public Class IdentityRoleConfiguration
        Inherits EntityTypeConfiguration(Of IdentityRole)
        Public Sub New()
            [Property](Function(r) r.Name).IsRequired()
        End Sub
    End Class
    End Namespace
    

    IdentityUserClaimConfiguration.vb

    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports System.Collections.Generic
    Imports System.Data.Entity.ModelConfiguration
    Imports System.Linq
    Imports System.Web
    
    Namespace myApp.models.Configurations
    Public Class IdentityUserClaimConfiguration
        Inherits EntityTypeConfiguration(Of IdentityUserClaim)
        Public Sub New()
            HasRequired(Function(u) u.User)
        End Sub
    End Class
    End Namespace
    

    IdentityUserLoginConfiguration.vb

    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports System.Collections.Generic
    Imports System.Data.Entity.ModelConfiguration
    Imports System.Linq
    Imports System.Web
    Imports System.Data.Entity
    
    Namespace myApp.models.Configurations
    Public Class IdentityUserLoginConfiguration
        Inherits EntityTypeConfiguration(Of IdentityUserLogin)
        Public Sub New()
            HasKey(Function(l) New With { _
                l.UserId, _
                l.LoginProvider, _
                l.ProviderKey _
            })
            HasRequired(Of IdentityUser)(Function(u) u.User)
        End Sub
    
    End Class
    End Namespace
    

    IdentityUserRoleConfiguration.vb

    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports System.Collections.Generic
    Imports System.Data.Entity.ModelConfiguration
    Imports System.Linq
    Imports System.Web
    
    Namespace CustomUser.Models.Configurations
    Public Class IdentityUserRoleConfiguration
        Inherits EntityTypeConfiguration(Of IdentityUserRole)
        Public Sub New()
            HasKey(Function(r) New With { _
                r.UserId, _
                r.RoleId _
            })
        End Sub
    End Class
    End Namespace
    

    我想就是这样。添加迁移,更新数据库,然后我就走了。

    希望这可以帮助其他人,如果有人可以贡献,或者提出更好的方法,我很乐意谈谈!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多