C# Entityframework core不生成Update语句,尽管entry.IsModified=true

C# Entityframework core不生成Update语句,尽管entry.IsModified=true,c#,entity-framework-core,C#,Entity Framework Core,虽然我将2个属性设置为“dirty”,但它们不会保存到数据库中 嗯,根本没有创建Update语句 我错了什么 var modifiedTestTypesToUpdate = testTypes.Except(testTypesToAdd.Concat(testTypesToRemove).ToList(), new TestTypeComparer()).ToList(); foreach (var testType in modifiedTestTypesToUpdate) { var

虽然我将2个属性设置为“dirty”,但它们不会保存到数据库中

嗯,根本没有创建Update语句

我错了什么

var modifiedTestTypesToUpdate = testTypes.Except(testTypesToAdd.Concat(testTypesToRemove).ToList(), new TestTypeComparer()).ToList();
foreach (var testType in modifiedTestTypesToUpdate)
{
    var entry = context.Entry(testType);
    entry.Property(p => p.Name).IsModified = true;
    entry.Property(p => p.Weight).IsModified = true;
}

await context.SaveChangesAsync(); 

public class TestType
{
    public TestType()
    {
        Tests = new HashSet<Test>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int Weight { get; set; }
    public ISet<Test> Tests { get; set; }
    public Schoolyear Schoolyear { get; set; }  
    public Schoolclass Schoolclass { get; set; }
    public int SchoolclassId { get; set; }
    public Subject Subject { get; set; }
    public int SubjectId { get; set; }
    public int SchoolyearId { get; set; }
}

 modelBuilder.Entity<TestType>().HasAlternateKey(x => new { x.Name, x.SchoolclassId, x.SubjectId });
异常

  var modifiedTestTypesToUpdate = testTypes.Except(testTypesToAdd.Concat(testTypesToRemove).ToList(), new TestTypeComparer()).ToList();
            foreach (var testType in modifiedTestTypesToUpdate)
            {
                testType.Schoolclass = schoolclass; // Exception occurs here firstly
                testType.Subject = subject;
                testType.Schoolyear = schoolyear;
                var entry = context.Entry(testType).State = EntityState.Modified;

            }
错误:
System.InvalidOperationException:实体类型“TestType”上的属性“SchoolclassId”是键的一部分,因此无法修改或标记为已修改。

如果我没有将这3个导航属性分配给测试类型,那么我将得到另一个异常,如:

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_TestTypes_Schoolyears_SchoolyearId". The conflict occurred in database "TGBData", table "dbo.Schoolyears", column 'Id'.
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_TestTypes_Schoolyears_SchoolyearId". The conflict occurred in database "TGBData", table "dbo.Schoolyears", column 'Id'.
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_TestTypes_Schoolyears_SchoolyearId". The conflict occurred in database "TGBData", table "dbo.Schoolyears", column 'Id'.
更新

现在,我的Update语句在Savechanges之前附加了3个导航道具后工作,我得到了这个生成的Update语句:

Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (6ms) [Parameters=[@p2='?', @p0='?', @p1='?', @p5='?', @p3='?', @p4='?', @p8='?', @p6='?', @p7='?'], CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
UPDATE [TestTypes] SET [SchoolyearId] = @p0, [Weight] = @p1
WHERE [Id] = @p2;
SELECT @@ROWCOUNT;
UPDATE [TestTypes] SET [SchoolyearId] = @p3, [Weight] = @p4
WHERE [Id] = @p5;
SELECT @@ROWCOUNT;
UPDATE [TestTypes] SET [SchoolyearId] = @p6, [Weight] = @p7
WHERE [Id] = @p8;
SELECT @@ROWCOUNT;

Update语句中究竟为什么不包括名称字段?

我假设您的实体也有一个键,您需要将其标记为entry.State=EntityState.Modified;这种将整个实体的状态标记为我以前使用过的修改状态的方法。我不想这样做,因为我已经将导航属性添加到testType实体中,并且我无法将这些导航道具设置为已修改。我得到的这个错误是一个例子:System.InvalidOperationException:实体类型“TestType”上的属性“SchoolclassId”是键的一部分,因此无法修改或标记为modifiedI用更多信息更新了我的问题!这些实体是否已经在上下文中被跟踪?是的,所有3个导航属性都是通过上下文跟踪的。附加(…)我假设您的实体也有一个键,您需要将其标记为entry.State=EntityState.Modified;这种将整个实体的状态标记为我以前使用过的修改状态的方法。我不想这样做,因为我已经将导航属性添加到testType实体中,并且我无法将这些导航道具设置为已修改。我得到的这个错误是一个例子:System.InvalidOperationException:实体类型“TestType”上的属性“SchoolclassId”是键的一部分,因此无法修改或标记为modifiedI用更多信息更新了我的问题!是否已在上下文中跟踪这些实体?是,所有3个导航属性都通过上下文跟踪。附加(…)