在迁移时使用 ef core 插入数据的问题
Issues with inserting data with ef core on migration
我正在尝试将自己的代码写入 ef 核心迁移 Up 方法,以将一些静态数据插入 table。简而言之,我想这样做。 INSERT INTO file_type (filetypeid, filetype) VALUES (10, 'Output File');
迁移中的代码
migrationBuilder.InsertData(
"file_type",
new string[] { "filetypeid", "filetype" },
new string[] { "integer", "varchar" },
new object[] { "10", "Output File" });
当我使用上面的代码时,我得到
当前提供程序不支持迁移数据操作中用于列 'file_type.filetypeid' 的存储类型 'integer'。
如果我省略数据类型参数 (3),我会得到
没有实体类型映射到数据操作中使用的 table 'file_type'。要么在模型中添加相应的实体类型,要么在数据操作中指定列类型。
型号
[Table("file_type")]
public class FileType
{
[Key]
[Column("filetypeid")]
public int FileTypeId { get; set; }
[Column("filetype")]
public string FileTypes { get; set; }
}
根据您与 ef core
一起使用的提供程序,您需要为您的提供程序指定相应的数据类型值。例如:使用 int
作为 SQL 服务器。更多信息:https://www.w3schools.com/sql/sql_datatypes.asp
使用解决了。感谢@Farzan 的帮助。
migrationBuilder.Sql("INSERT INTO file_type (filetypeid, filetype) VALUES (10, 'Output File');", false);
我正在尝试将自己的代码写入 ef 核心迁移 Up 方法,以将一些静态数据插入 table。简而言之,我想这样做。 INSERT INTO file_type (filetypeid, filetype) VALUES (10, 'Output File');
迁移中的代码
migrationBuilder.InsertData(
"file_type",
new string[] { "filetypeid", "filetype" },
new string[] { "integer", "varchar" },
new object[] { "10", "Output File" });
当我使用上面的代码时,我得到
当前提供程序不支持迁移数据操作中用于列 'file_type.filetypeid' 的存储类型 'integer'。
如果我省略数据类型参数 (3),我会得到
没有实体类型映射到数据操作中使用的 table 'file_type'。要么在模型中添加相应的实体类型,要么在数据操作中指定列类型。
型号
[Table("file_type")]
public class FileType
{
[Key]
[Column("filetypeid")]
public int FileTypeId { get; set; }
[Column("filetype")]
public string FileTypes { get; set; }
}
根据您与 ef core
一起使用的提供程序,您需要为您的提供程序指定相应的数据类型值。例如:使用 int
作为 SQL 服务器。更多信息:https://www.w3schools.com/sql/sql_datatypes.asp
使用解决了。感谢@Farzan 的帮助。
migrationBuilder.Sql("INSERT INTO file_type (filetypeid, filetype) VALUES (10, 'Output File');", false);