System.Data.SqlClient.SqlException (0x80131904): 列名无效 'PayasYouGo'

System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'PayasYouGo'

我目前正在关注 Mosh Hamedami 关于使用代码优先迁移为数据库播种的教程。不幸的是我遇到了这个错误 System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'PayasYouGo'。我曾尝试重新创建我的模型,但似乎没有任何效果。

模特:

public class MembershipType

    {
        public byte Id { get; set; }
        public short SignUpFee { get; set; }
        public byte DurationInMonths { get; set; }
        public byte DiscountRate { get; set; }
        public string MembershipName { get; set; }
    }

迁移代码

public partial class PopulateMembership : DbMigration
{
    public override void Up()
    {
        Sql("INSERT INTO MembershipTypes (Id, SignUpFee, DurationInMonths, DiscountRate, MembershipName) VALUES (1, 0, 0, 0, PayasYouGo)");
        Sql("INSERT INTO MembershipTypes (Id, SignUpFee, DurationInMonths, DiscountRate, MembershipName) VALUES (2, 30, 1, 10, Monthly)");
        Sql("INSERT INTO MembershipTypes (Id, SignUpFee, DurationInMonths, DiscountRate, MembershipName) VALUES (3, 90, 3, 15, Quarterly)");
        Sql("INSERT INTO MembershipTypes (Id, SignUpFee, DurationInMonths, DiscountRate, MembershipName) VALUES (4, 300, 12, 20, Annual)");
    }

    public override void Down()
    {
    }
}

The Database Migrated

我在这里做错了什么?是什么导致了这个错误?这个错误是什么意思?提前致谢

您想提供一个 字符串,但却给了 SQL 服务器一个 名称

SQL 服务器中的字符串文字被单引号字符包围:

    Sql(@"INSERT INTO MembershipTypes
          (Id, SignUpFee, DurationInMonths, DiscountRate, MembershipName)
          VALUES (1, 0, 0, 0, 'PayasYouGo')");

(如果它按您预期的那样工作,请考虑如果您想插入一个 包含 逗号、括号、白色 space 的字符串,您将如何挣扎,等等)