如何以代码优先方法在现有数据库 table 中添加列?
How to add columns in existing database table in code first approach?
我的解决方案中有两个项目,一个是简单的 mvc 项目,另一个是 web api.I 用过 Entity Framework。我还使用了 asp.net 身份,它生成了在 AspNetUsers table 中有很多列的本地数据库。现在我想从 AspNetUsers table 添加和删除一些列。
我在我的项目中包含了 Migrations 文件夹,其中包含 configuration.cs 和初始 create.cs 类
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Email = c.String(maxLength: 256),
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(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
我也曾尝试使用程序包管理器控制台更新数据库,但没有任何反应。
那么如何在 AspNetUsers table 中添加另一列?
提前致谢。
无需添加代码即可自行创建 Table。 Migrations 可以为您做一切。
public class ApplicationUser : IdentityUser
{
... Your Codes ...
public string FirstName { get; set; }
public string LastName { get; set; }
}
并且 运行 PackageManager 中的代码:
add-migration somenotehere
它将为您的数据库创建迁移。
那么您所要做的就是将更改提交给ddl。
因此 运行 PackageManager 中的这段代码:
update-database
希望有用。
我的解决方案中有两个项目,一个是简单的 mvc 项目,另一个是 web api.I 用过 Entity Framework。我还使用了 asp.net 身份,它生成了在 AspNetUsers table 中有很多列的本地数据库。现在我想从 AspNetUsers table 添加和删除一些列。 我在我的项目中包含了 Migrations 文件夹,其中包含 configuration.cs 和初始 create.cs 类
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Email = c.String(maxLength: 256),
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(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
我也曾尝试使用程序包管理器控制台更新数据库,但没有任何反应。 那么如何在 AspNetUsers table 中添加另一列? 提前致谢。
无需添加代码即可自行创建 Table。 Migrations 可以为您做一切。
public class ApplicationUser : IdentityUser
{
... Your Codes ...
public string FirstName { get; set; }
public string LastName { get; set; }
}
并且 运行 PackageManager 中的代码:
add-migration somenotehere
它将为您的数据库创建迁移。 那么您所要做的就是将更改提交给ddl。
因此 运行 PackageManager 中的这段代码:
update-database
希望有用。