【问题标题】:asp net Identity 2.0 beta1 with extended Guid Primary Key EF Migrations issue带有扩展 Guid 主键 EF 迁移问题的 asp net Identity 2.0 beta1
【发布时间】:2014-02-25 06:13:19
【问题描述】:

我正在使用 ASP Net Identity 2.0 beta1 并按照以下说明操作: http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx 将主键扩展为 GUID 而不是标准字符串。我必须这样做才能使外键与现有表一起使用。使用代码优先。

我的数据库上下文 OnModelCreating 中没有任何额外代码。

我遇到的问题是,在尝试添加迁移时出现错误:

MyApp.Business.GuidUserLogin: : EntityType 'GuidUserLogin' 没有定义键。定义此 EntityType 的键。

MyApp.Business.GuidUserRole: : EntityType 'GuidUserRole' 没有定义键。定义此 EntityType 的键。

GuidUserLogins: EntityType: EntitySet 'GuidUserLogins' 基于没有定义键的类型'GuidUserLogin'。

GuidUserRoles: EntityType: EntitySet 'GuidUserRoles' 基于没有定义键的类型'GuidUserRole'。

【问题讨论】:

    标签: identity entity-framework-migrations asp.net-identity


    【解决方案1】:

    修复方法是在 OnModelCreating 覆盖下的 DbContext 中手动添加键:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {   
            modelBuilder.Entity<GuidUserRole>().HasKey( x=> new {
                x.RoleId,
                x.UserId
            });
    
            modelBuilder.Entity<GuidUserLogin>().HasKey(x => new
            {
                x.UserId,
                x.ProviderKey,
                x.LoginProvider
            });
    }
    

    添加后,ef 迁移工作正常,我能够创建用户角色等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 2022-07-15
      • 2010-10-31
      • 2011-03-18
      • 2014-12-14
      • 2019-06-19
      相关资源
      最近更新 更多