C# .SaveChanges()未保存到数据

C# .SaveChanges()未保存到数据,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我知道以前有人问过这个问题,但我仍然完全不明白为什么这个对象没有保存到数据库中。我使用两种服务方法来填充一个表。CreateFirstCategory()工作正常并保存到数据库中,但由于某些原因,CreateFirstItem()将初始化新对象,但不会将其保存到数据库中。你知道为什么吗 控制器 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using

我知道以前有人问过这个问题,但我仍然完全不明白为什么这个对象没有保存到数据库中。我使用两种服务方法来填充一个表。CreateFirstCategory()工作正常并保存到数据库中,但由于某些原因,CreateFirstItem()将初始化新对象,但不会将其保存到数据库中。你知道为什么吗

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Programming_Library.Data;
using Programming_Library.Models;
using Programming_Library.Services;

namespace Programming_Library.Controllers
{
    public class DashboardController : Controller
    {
        private readonly ApplicationDbContext _context;
        private readonly ICreateService _createService;

        public DashboardController(ApplicationDbContext context, ICreateService createService)
        {
            _context = context;
            _createService = createService;
        }
        public IActionResult Index()
        {
            _createService.CreateFirstCategory();
            _createService.CreateFirstItem();



            return View();
        }
    }
}
服务

using Programming_Library.Data;
using Programming_Library.Models;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Programming_Library.Services
{
    public class CreateService : ICreateService
    {
        private readonly ApplicationDbContext _context;
        public CreateService(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<Category> CreateFirstCategory()
        {
            var categoryCount = _context.Category.Count();
            if (categoryCount == 0)
            {
                var category = new Category
                {
                    Id = 0,
                    Name = "Instructions",
                };
                await _context.Category.AddAsync(category);
                await _context.SaveChangesAsync();
                return category;
            }
            return null;
        }

        public async Task<Item> CreateFirstItem()
        {
            int itemCount = _context.Item.Count();
            if (itemCount == 0)
            {
                var item = new Item
                {
                    CategoryId = 0,
                    Id = 0,
                    Name = "Getting Started",
                    Updated = DateTime.Now,
                };
                await _context.Item.AddAsync(item);
                await _context.SaveChangesAsync();
                return item;
            }
            return null;
        }
}
使用编程库数据;
使用编程库模型;
使用制度;
使用System.Linq;
使用System.Threading.Tasks;
命名空间编程\u Library.Services
{
公共类CreateService:ICreateService
{
私有只读应用程序的bContext\u上下文;
公共CreateService(ApplicationDbContext上下文)
{
_上下文=上下文;
}
公共异步任务CreateFirstCategory()
{
var categoryCount=_context.Category.Count();
如果(类别计数==0)
{
var类别=新类别
{
Id=0,
Name=“说明”,
};
wait\u context.Category.AddAsync(Category);
wait_context.SaveChangesAsync();
退货类别;
}
返回null;
}
公共异步任务CreateFirstItem()
{
int itemCount=_context.Item.Count();
如果(itemCount==0)
{
var项目=新项目
{
类别ID=0,
Id=0,
Name=“入门”,
Updated=DateTime。现在,
};
wait\u context.Item.AddAsync(Item);
wait_context.SaveChangesAsync();
退货项目;
}
返回null;
}
}
模型

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
名称空间编程\u Library.Models
{
公共类项目
{
公共项目()
{
Sections=新的HashSet();
}
[必需]
[长度(50)]
公共字符串名称{get;set;}
公共int Id{get;set;}
[数据类型(DataType.Date)]
公共DateTimeOffset已更新{get;set;}
公共字符串PLUserID{get;set;}
public int CategoryId{get;set;}
public int languageId{get;set;}
public int FrameworkId{get;set;}
公共int ItemTypeId{get;set;}
公共ItemType ItemType{get;set;}
公共框架框架{get;set;}
公共语言{get;set;}
公共虚拟ICollection节{get;set;}
}
}


您有两个异步任务CreateFirstCategory和CreateFirstItem。它们正在同时创建和运行。您需要一种方法来验证其中一个异步任务是否在第二次运行之前完成。

您有两个异步任务CreateFirstCategory和CreateFirstItem。它们正在同时创建和运行。您需要一种方法来验证这一点其中一个异步任务在第二次运行之前完成。

谢谢,就是这样。我把它们做成了同步方法,现在没有线程冲突。谢谢,就是这样。我把它们做成了同步方法,现在没有线程冲突。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Programming_Library.Models
{
    public class Item
    {
        public Item()
        {
            Sections = new HashSet<Section>();
        }

        [Required]
        [StringLength(50)]
        public string Name { get; set; }
        public int Id { get; set; }

        [DataType(DataType.Date)]
        public DateTimeOffset Updated { get; set; }

        public string PLUserID { get; set; }
        public int CategoryId { get; set; }
        public int languageId { get; set; }
        public int FrameworkId { get; set; }
        public int ItemTypeId { get; set; }

        public ItemType ItemType { get; set; }
        public Framework Framework { get; set; }
        public language language { get; set; }

        public virtual ICollection<Section> Sections { get; set; }

    }
}


public IActionResult Index()
{
    _createService.CreateFirstCategory();
    _createService.CreateFirstItem();


    return View();
}
public async Task<Category> CreateFirstCategory()
public async Task<Item> CreateFirstItem()
public async Task<IActionResult> Index()
{
     await _createService.CreateFirstCategory();
     await _createService.CreateFirstItem();

     return View();
}