Nullable fields are created when customizing IdentityUser class in asp.net Identity

天涯浪子 提交于 2019-12-01 08:36:47

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!