c# - 在迁移时使用 ef core 插入数据的问题
问题描述
我正在尝试将自己的代码写入 ef core migration Up 方法,以将一些静态数据插入表中。简而言之,我想这样做。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”的存储类型“整数”。
如果我省略了数据类型参数 (3) 我得到
没有实体类型映射到数据操作中使用的表“file_type”。要么将对应的实体类型添加到模型中,要么在数据操作中指定列类型。
模型
[Table("file_type")]
public class FileType
{
[Key]
[Column("filetypeid")]
public int FileTypeId { get; set; }
[Column("filetype")]
public string FileTypes { get; set; }
}
解决方案
通过使用解决了它。感谢@Farzan 的帮助。
migrationBuilder.Sql("INSERT INTO file_type (filetypeid, filetype) VALUES (10, 'Output File');", false);
推荐阅读
- google-sheets - Vlookup 或索引并在 Google 表格的下一列中返回多个相邻的行
- nginx - 使用 Cloudflare 的 NGINX 反向代理
- bash - /bin/cp ~/.bashrc ~/my_bashrc: 没有那个文件或目录
- google-cloud-platform - 如何使用 CLI 将 SSH 密钥添加到 Google Cloud
- excel - 使用 SUMPRODUCT 对矩阵的不同行求和
- google-apps-script - 有没有办法将文件和文件夹存档到 Google Drive 中的特定文件夹并保留文件夹路径?
- sql - SQL - 请求五个表
- python - 如何将txt读取为没有符号的数据框
- react-native - Iphone 如何在 React Native 中存储和自动填写密码/电子邮件?
- javascript - 如何在javascript中将HTML文件内容作为字符串获取