我是一个经典的程序员,是仿制药的新手,这是一个asp.net MVC5示例应用程序,用于学习使用流畅的迁移器库集成授权(用户/角色)。我想在创建表时将一些示例数据添加到表中(使用迁移器控制台工具)。
收到编译错误:当前上下文中不存在USERNAME
我应该添加什么来使用部分或任何示例:
Insert.IntoTable方法?
(感谢)
namespace SampleApp.Migrations
{
[Migration(1)]
public class AuthMigrations:Migration
{
public override void Up()
{
Create.Table("users").
WithColumn("ID").AsInt32().Identity().PrimaryKey().
WithColumn("USERNAME").AsString(128).
WithColumn("EMAIL").AsCustom("VARCHAR(128)").
WithColumn("PASSWORD_HASH").AsString(128);
Create.Table("roles").
WithColumn("ID").AsInt32().Identity().PrimaryKey().
WithColumn("NAME").AsString(128);
Create.Table("role_users").
WithColumn("ID").AsInt32().Identity().PrimaryKey().
WithColumn("USER_ID").AsInt32().ForeignKey("users", "ID").OnDelete(Rule.Cascade).
WithColumn("ROLE_ID").AsInt32().ForeignKey("roles", "ID").OnDelete(Rule.Cascade);
//Error:The name 'USERNAME' does not exist in the current context
Insert.IntoTable("users").Row(new { USERNAME:"superadmin",EMAIL:"superadmin@mvcapp.com",PASSWORD_HASH:"dfgkmdglkdmfg34532+"});
Insert.IntoTable("users").Row(new { USERNAME:"admin",EMAIL:"admin@mvcapp.com",PASSWORD_HASH:"dfgkmdglkdmfg34532+"});
}
public override void Down()
{
Delete.Table("role_users");
Delete.Table("roles");
Delete.Table("users");
}
}
和
namespace SampleApp.Models
{
public class User
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual string EMail { get; set; }
public virtual string passwordhash { get; set; }
}
public class UserMap : ClassMapping<User>
{
public UserMap()
{
Table("Users");
Id(x => x.Id, x => x.Generator(Generators.Identity));
Property(x => x.Username, x => x.NotNullable(true));
Property(x => x.EMail, x => x.NotNullable(true));
Property(x=>x.passwordhash,x=>
{
x.Column("PASSWORD_HASH");
x.NotNullable(true);
});
}
}
}
答案 0 :(得分:5)
在C#中,必须在对象初始值设定项中使用等号(“=”)而不是冒号(“:”)。
Insert.IntoTable("users").Row(new { USERNAME = "superadmin",EMAIL = "superadmin@mvcapp.com",PASSWORD_HASH = "dfgkmdglkdmfg34532+"});
Insert.IntoTable("users").Row(new { USERNAME = "admin",EMAIL = "admin@mvcapp.com",PASSWORD_HASH = "dfgkmdglkdmfg34532+"});