虽然我将2个属性设置为"脏"他们没有保存到数据库中。
根本没有创建更新声明...
我错了什么?
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 });
更新
因为它建议将条目的状态设置为State.Modified然后我回答我在遇到问题之前尝试了这个,该实体上的导航属性也被标记为已修改,导致 Exception < /强>:
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: The property 'SchoolclassId' on entity type 'TestType' is part of a key and so cannot be modified or marked as modified.
如果我没有将这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语句中?