[英]EntityFramework and SQL Server Remove Related Entity
I am having a weird issue with my understanding of Entity Framework.我对实体框架的理解有一个奇怪的问题。
I have the following sample objects recording as data in my SQL Database.我有以下示例对象在我的 SQL 数据库中记录为数据。
public class Competition
{
public long Id { get; set;}
public string Name { get; set; }
}
public class SeasonCompetition
{
public long Id { get; set;}
public string Name { get; set; }
public Competition Competition { get; set; }
}
Now, I create a new instance of Competition
and save it to the database.现在,我创建一个新的Competition
实例并将其保存到数据库中。 I then create a new instance of SeasonCompetition
relating to the created Competition
.然后我创建一个与创建的Competition
相关的SeasonCompetition
新实例。
Competition competition = new Competition();
competition.Name = "Test";
_context.Add(competition);
_context.SaveChanges();
SeasonCompetition seasoncompetition = new SeasonCompetition();
seasoncompetition.Name = "Test Season Comp";
seasoncompetition.Competiton = competition;
_context.Add(seasoncompetition);
_context.SaveChanges();
This successfully writes a record to the SeasonCompetitions
table looking a bit like this.这成功地将一条记录写入到SeasonCompetitions
表中,看起来有点像这样。
Id ID | Name姓名 | SeasonCompetitionId SeasonCompetitionId |
---|---|---|
1 1 | Test Season Competition测试季比赛 | 1 1 |
Now, I want to remove the relationship so this SeasonCompetition
does not relate to the Competition
record现在,我想删除关系,所以这个SeasonCompetition
与Competition
记录无关
seasoncompetition.Competition = null;
_context.Update(seasoncompetition);
_context.SaveChanges();
If I put a breakpoint in and examine the seasoncompetition
object after SaveChanges()
, I can see seasoncompetition.Competition = null
, but looking at the database, it still shows this.如果我在SaveChanges()
之后放置一个断点并检查seasoncompetition
对象,我可以看到seasoncompetition.Competition = null
,但查看数据库,它仍然显示这一点。
Id ID | Name姓名 | SeasonCompetitionId SeasonCompetitionId |
---|---|---|
1 1 | Test Season Competition测试季比赛 | 1 1 |
How do I update the SeasonCompetition
record to remove the relationship to Competition
but still keep the Competition
record in place in the database?如何更新SeasonCompetition
记录以删除与Competition
的关系,但仍将Competition
记录保留在数据库中?
Using Visual Studio 2022 and .NET 5使用 Visual Studio 2022 和 .NET 5
Using Microsoft.EntityFrameworkCore - make 'Competition' nullable :使用Microsoft.EntityFrameworkCore - 使“竞争”可为空:
public class SeasonCompetition
{
public long Id { get; set; }
public string Name { get; set; }
public Competition? Competition { get; set; }
}
Calling _context.Update(seasoncompetition)
doesn't do anything in this case.在这种情况下,调用_context.Update(seasoncompetition)
不会做任何事情。 This is how you should do it:你应该这样做:
//create
var competition = new Competition
{
Name = $"Competition {DateTime.Now.Millisecond}"
};
_context.Competitions.Add(competition);
_context.SaveChanges();
var season = new SeasonCompetition
{
Competition = competition,
Name = $"Season {DateTime.Now.Millisecond}"
};
_context.SeasonCompetitions.Add(season);
_context.SaveChanges();
//Created season & competition
season.Competition = null;
_context.SaveChanges();
//Removed competition from season
This feature is called change tracking whereby the data context automatically can detect when a model has changed if it is associated to the data context.此功能称为更改跟踪,如果模型与数据上下文相关联,则数据上下文可以自动检测模型何时发生更改。 You can read more about this here: https://docs.microsoft.com/en-us/ef/core/change-tracking/您可以在此处阅读有关此内容的更多信息: https ://docs.microsoft.com/en-us/ef/core/change-tracking/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.