简体   繁体   English

实体框架代码优先不会将身份设置为是

[英]Entity Framework Code First will not set Identity to Yes

I have classes for a Code First implementation and I am trying to get it to create the table so that the Id Field is set to Identity = yes. 我有一个代码优先实现的类,我试图让它创建表,以便将ID字段设置为Identity = yes。 Not matter what I do though it always seems to be set to no. 不管我做什么,尽管总是设置为no。

I've tried adding the .HasKey and the HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) on the Id property as follows: 我尝试在Id属性上添加.HasKey和HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity),如下所示:

 public class ApplicationDbContext : IdentityDbContext<User, Role, Guid, UserLogin, UserRole, UserClaim>
 {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.HasDefaultSchema("dbo");

                      modelBuilder.Entity<User>().Map(c =>
        {
            c.ToTable("User");
            c.Property(p => p.Id).HasColumnName("UserId");                
            c.Properties(p => new
            {
                p.AccessFailedCount,
                p.Email,
                p.EmailConfirmed,
                p.PasswordHash,
                p.PhoneNumber,
                p.PhoneNumberConfirmed,
                p.TwoFactorEnabled,
                p.SecurityStamp,
                p.LockoutEnabled,
                p.LockoutEndDateUtc,
                p.UserName,
                p.FirstName,
                p.MiddleName,
                p.LastName,
                p.IsActive,
                p.LastLogin,
                p.CreatedBy,
                p.CreatedOn,
                p.LastModifiedBy,
                p.LastModifiedOn
            });
        }).HasKey(c => c.Id);
        modelBuilder.Entity<User>().HasMany(c => c.Logins).WithOptional().HasForeignKey(c => c.UserId);
        modelBuilder.Entity<User>().HasMany(c => c.Claims).WithOptional().HasForeignKey(c => c.UserId);
        modelBuilder.Entity<User>().HasMany(c => c.Roles).WithRequired().HasForeignKey(c => c.UserId);
        modelBuilder.Entity<User>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<User>().HasKey(p => p.Id);

I've also set the Attribute on the Id Property in the Class to be Identity 我还将类的Id属性上的属性设置为Identity

    public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>, IEntity
    {
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, Guid> manager, string authenticationType)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        // Add custom user claims here
        return userIdentity;
    }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public override Guid Id { get; set; }
    [MaxLength(256)]
    public string FirstName { get; set; }
    [MaxLength(256)]
    public string MiddleName { get; set; }
    [MaxLength(256)]
    public string LastName { get; set; }
    public bool IsActive { get; set; }
    public DateTime? LastLogin { get; set; }
    public Guid? CreatedBy { get; set; }
    public DateTime? CreatedOn { get; set; }
    public Guid? LastModifiedBy { get; set; }
    public DateTime? LastModifiedOn { get; set; }         
}
}

The Up() method in the actual Migration comes out as follows: 实际迁移中的Up()方法如下所示:

CreateTable(
            "dbo.User",
            c => new
                {
                    **UserId = c.Guid(nullable: false, identity: true),**
                    FirstName = c.String(maxLength: 256),
                    MiddleName = c.String(maxLength: 256),
                    LastName = c.String(maxLength: 256),
                    IsActive = c.Boolean(nullable: false),
                    LastLogin = c.DateTime(),
                    CreatedBy = c.Guid(),
                    CreatedOn = c.DateTime(),
                    LastModifiedBy = c.Guid(),
                    LastModifiedOn = c.DateTime(),
                    Email = c.String(),
                    EmailConfirmed = c.Boolean(nullable: false),
                    PasswordHash = c.String(),
                    SecurityStamp = c.String(),
                    PhoneNumber = c.String(),
                    PhoneNumberConfirmed = c.Boolean(nullable: false),
                    TwoFactorEnabled = c.Boolean(nullable: false),
                    LockoutEndDateUtc = c.DateTime(),
                    LockoutEnabled = c.Boolean(nullable: false),
                    AccessFailedCount = c.Int(nullable: false),
                    UserName = c.String(),
                })
            .PrimaryKey(t => t.UserId);

SQL服务器

As you can see though, when the script is ran the Identity is set to No. 如您所见,运行脚本时,“身份”设置为“否”。

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

相关问题 如何首先在实体框架代码中设置身份主键的起始值? - How to set starting value for an Identity primary key in Entity Framework code first? 使用现有数据库和实体框架的代码优先:当IDENTITY_INSERT设置为OFF时,无法为表中的标识列插入显式值 - Code First With Existing Database, Entity Framework: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF 如何在Entity Framework代码优先中将列更改为唯一身份 - How to alter column to unique identity in Entity Framework Code First 从表Code First Entity Framework 6中删除标识插入 - Remove Identity Insert from table Code First Entity Framework 6 实体框架代码优先:使用种子和增量值初始化标识列 - Entity Framework Code First: Initialize Identity Column with Seed and Increment Values 实体框架代码优先集合映射 - Entity Framework Code First Set Mapping 实体框架6代码First int Identity第一列的值为零 - Entity Framework 6 Code First int Identity column value is zero for first row 如何首先使用Entity Framework代码为用户设置语言选择 - How to set up selection of language for a user with Entity Framework code first 如何在Entity Framework代码优先中设置空外键 - How to set null foreign key in Entity Framework code-first 实体框架代码优先迁移和设置 <T> 方法 - Entity Framework code first migration and the Set<T> method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM