0
我想用.NET核心製作一個簡單的程序,該程序應該將插件和關聯的類別保存到MySQL數據庫中。使用EFcore不添加到數據庫中的對象列表
這兩個類存在:
public class Plugin
{
public string Name { get; set; }
public string Description { get; set; }
public List<Category> Categories { get; set; }
[Key]
public string ProjName { get; set; }
public string DownloadURL { get; set; }
}
public class Category
{
[Key]
public string UniqueName { get; set; }
public string PrettyName { get; set; }
}
我使用這個語境:
public class Context : DbContext
{
public DbSet<Plugin> Plugins { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseMySql(@"Server=XXX");
}
簡化代碼(沒有HttpClient的東西)看起來是這樣的:
public static async Task GetPluginsOnPage(string baseUri)
{
foreach (var plugin in pluginsOnPage)
{
List<Category> categories = new List<Category>();
var nameHtml = "**";
var descHtml = "**";
var projName = "**";
foreach (var x in plugin.***)
{
categories.Add(new Category() {PrettyName = "", UniqueName = "**" });
}
var downloadUri = "**";
await Ctx.Plugins.AddAsync(new Plugin { Categories = categories, Description = descHtml, Name = nameHtml, ProjName = projName, DownloadURL = downloadUri });
}
await Ctx.SaveChangesAsync();
}
結果數據庫:
請注意,已添加20個插件,但沒有分類。