MVC5 VB.Net - 单个dbContext和迁移

时间:2014-03-27 17:18:13

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

这与最佳实践有关,但也有一些实际问题。

我是MVC和.Net的新手,以前是一名PHP开发人员。我总体上喜欢它,而Entity Framework / Code First有点特别。

所以我在第一个项目上做了一些工作,我的第一个问题是,似乎没有很多关于VB的文档 - 我不能使用C#,因为这里的其他开发人员都是VB人员。我可以解决整个问题,但有人知道MVC5& amp; VB?

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

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

本周简介已更改,我现在需要包含更多用户数据,并将我的用户链接到其他连接中的其他表。

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

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

我似乎能够一次为我的一个dbContexts设置迁移,这让我觉得我需要在一个dbContext中拥有所有内容。我已经看到有关在我自己的dbContext类中继承IdentityDbContext的讨论,但我无法使其工作 - 只有Identity表,而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>

有人能指出我正确的方向吗?

提前致谢

西蒙

1 个答案:

答案 0 :(得分:0)

我最终想出来了,我会在这里发布基础知识以防止其他任何人受益。

在我自己的数据类中,我继承了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 Sub:

中的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

我认为就是这样。添加迁移,更新数据库,我不在。

希望这可以帮助其他人,如果有人可以做出贡献,或建议更好的方式,我会喜欢说话!