使用Fluent迁移器将初始行添加到表中 [英] adding initial rows into tables using Fluent migrator

查看:118
本文介绍了使用Fluent迁移器将初始行添加到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个经典的程序员,是通用类的新手,这是一个asp.net MVC5示例应用程序,用于学习使用流利的migrator lib集成授权(用户/角色)的目的.我想在创建表时将一些示例数据添加到表中(使用迁移器控制台工具).

Im a classic programmer that is newbie at generics and this is an asp.net MVC5 sample application for learning purposes of integrating authorization (users/roles) using fluent migrator lib. I wantto add some sample datas into tables as they created (using migrator console tool).

遇到编译错误:USERNAME在当前上下文中不存在
我应该在使用部分或以下任何示例中添加什么:Insert.IntoTable方法?

getting compilation error: USERNAME does not exist in the current context
what should I add in to using section or any example of: Insert.IntoTable method ?

(谢谢)

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);
            });
        }
    }
}

推荐答案

在C#中,必须在对象初始值设定项中使用等号("=")而不是冒号(:").

In C#, you must use an equals sign ("=") in the object initializer instead of a colon (":").

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+"});

这篇关于使用Fluent迁移器将初始行添加到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆