2014-03-27 24 views
1

这与最佳实践有些关系,但也有一些实际问题。MVC5 VB.Net - 单个dbContext和迁移

我是MVC和.Net的新手,之前一直是PHP开发人员。我非常喜欢它,实体框架/ Code First有点特别。

所以,我已经做了一些工作,我的第一个项目,我的第一个问题是,似乎没有成为VB大量的文档资料 - 我不能使用C#,因为其他开发商都在这里所有的VB人。我可以解决这个问题,但是有谁知道MVC5 & VB是一个很好的资源,书籍还是在线?

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

目前我有两个数据连接,一个用于身份的东西,一个用于一切(如在MVC网站的例子)。

本周简短已经改变,我现在需要包括更多的用户数据,以及我在其他方面我的用户链接到其他表。

例如,我需要添加一个clientCompany表,并在我的用户表中有相应的字段。给定的客户公司将有salesRegions,其中每个都有postCodes等。

因此,要将我的Identity用户表加入到我的其他人,并设置外键,他们是否需要占用相同的连接,以及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> 

任何人都可以点我请在正确的方向?

在此先感谢

西蒙

回答

0

我想通了最后,我将在这里发布的基本知识的情况下,它可以造福别人。

在我自己的数据类,我继承的DbContext并添加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。注意:MyDataClassNew()子:

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 

最后,我添加了一个“配置”文件夹中的“模型”文件夹,并添加以下五个类别。这是我以前失踪的部分。

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 

我想这就是它。添加迁移,更新数据库,并且我离开了。

希望这可以帮助其他人,如果任何人都可以贡献,或建议更好的方法,我很乐意谈!