本文介绍了在asp.net身份中定制IdentityUser类时,会创建可空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class ApplicationUser:IdentityUser 
{
public ApplicationUser()
{
IsBlocked = false;
}

public bool IsBlocked {get;组;
}

问题是:使用代码首次迁移时,会创建附加字段空。
如果我删除数据库并重新创建它,则相同。

  CREATE TABLE [dbo]。[AspNetUsers](
[Id] NVARCHAR(128)NOT NULL,
[UserName] NVARCHAR(MAX)NULL,
[PasswordHash] NVARCHAR(MAX)NULL,
[SecurityStamp] NVARCHAR(MAX)NULL ,
[IsConfirmed] BIT NOT NULL,
[IsBlocked] BIT NULL,
[Discriminator] NVARCHAR(128)NOT NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED [Id] ASC)
);

我该如何解决?



我在同一个DbContext中的其他类中有布尔字段,并且它们都被创建为非空(应该是)。

解决方案

原来的答案如下,假设你有一个抽象的基类,因此使用TPC,或者可能是TPT,如果你在具体类中指定了 [Table] 属性,比。



但是,如果您使用的是非抽象基类,并且未指定 [Table] ApplicationUser ,因此您的 ApplicationUser IdentityUser 映射到单表,那么您正在使用TPH方案。在这种情况下,单个表中的子类中的任何字段都将为空。更改此项的唯一方法是切换到TPC或TPT。



原始答案



将属性:

  [必需] 
public bool IsBlocked {get;组; }

您的迁移应该使它成为一个 NON NULL 列。



但是,如果您的表中已有数据,则会导致问题,因为它不会知道要创建的默认值。在这种情况下,我编辑迁移以首先使其成为 NULL 列,然后运行命令设置我想要的值,然后设置使它成为 NON NULL

  AddColumn(dbo.MyTable,MyColumn,c => c.Boolean()); 
Sql(UPDATE MyTable SET MyColumn = 1);
AlterColumn(dbo.MyTable,MyColumn,c => c.Boolean(nullable:false));


I am trying to customize IdentityUser class in asp.net identity.

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        IsBlocked = false;
    }

    public bool IsBlocked { get; set; }
}

The problem is: when using code first migrations, the additional field is created nullable.The same if I drop database and recreate it.

CREATE TABLE [dbo].[AspNetUsers] (
    [Id]            NVARCHAR (128) NOT NULL,
    [UserName]      NVARCHAR (MAX) NULL,
    [PasswordHash]  NVARCHAR (MAX) NULL,
    [SecurityStamp] NVARCHAR (MAX) NULL,
    [IsConfirmed]   BIT            NOT NULL,
    [IsBlocked]     BIT            NULL,
    [Discriminator] NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);

How can I fix this?

I have boolean fields in other classes on the same DbContext, and they are all created not null (as they should).

解决方案

The original answer, below, assumed you had an abstract base class, and therefore were using TPC, or possibly TPT if you specified the [Table] attribute on the concrete class, rather than TPH.

However, if you are using a non-abstract base class and do not specify a [Table] on your ApplicationUser, and therefore your ApplicationUser and IdentityUser map to a single table, then you are using the TPH scenario. In this scenario any fields from a subclass will be nullable in your single table. The only way to change this is to switch to TPC or TPT.

Original answer

Put the [Required] attribute on your property:

[Required]
public bool IsBlocked { get; set; }

Your migration should then make it a NON NULL column.

However, if you already have data in your table this will cause issues as it won't know what default value to create. In this case I edit the migration to first make it a NULL column, then run a Sql command to set the values I want, and then AlterColumn make it a NON NULL column

AddColumn("dbo.MyTable", "MyColumn", c => c.Boolean());
Sql("UPDATE MyTable SET MyColumn = 1");
AlterColumn("dbo.MyTable", "MyColumn", c => c.Boolean(nullable: false));

这篇关于在asp.net身份中定制IdentityUser类时,会创建可空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:37