我正在使用asp.net core和sql server中的CSV文件创建数据库种子。这是一个多驱动的数据结构。对于某些实体,以下代码可以正确更新数据。当我创建一个新的csv文件并尝试使用一个新的实体时,它给我的值未找到错误。 吹我给我的代码示例:
Assembly Asm = Assembly.GetExecutingAssembly();
string[] names = Asm.GetManifestResourceNames();
foreach (var name in names) Debug.WriteLine(name);
string assemblyName = Asm.GetName().Name;
string emailTemplateName = "IndustryAPI.Setup.VendorRating.csv";
emailTemplateName = assemblyName + "." + emailTemplateName;
string resourceName5 = "IndustryAPI.Setup.VendorRating.csv";
using (Stream stream = Asm.GetManifestResourceStream(resourceName5))
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
CsvReader csvReader = new CsvReader(reader);
csvReader.Configuration.RegisterClassMap<VendorRatingMap>();
csvReader.Configuration.MissingFieldFound = null;
var Rat = csvReader.GetRecords<VendorRating>().ToArray();
foreach (VendorRating record in Rat)
{
customerList.Add(record);
}
}
}
builder.Entity<VendorRating>().HasData(customerList);
public class VendorRatingMap : ClassMap<VendorRating>
{
public VendorRatingMap()
{
Map(m => m.Id).Name("Id");
Map(m => m.VendorId).Name("VendorId");
Map(m => m.Name).Name("Name");
Map(m => m.Comment).Name("Comment");
Map(m => m.Value).Name("Value");
}
}
答案 0 :(得分:1)
确保Build Action
的{{1}}为VendorRating.csv
。
右键单击Embedded resource
->“构建操作”->“嵌入资源”。