简体   繁体   English

实体框架级联删除不起作用

[英]Entity Framework Cascade delete doesn't work

For some reason I'm not able to use Cascade Delete with Entity Framework. 由于某些原因,我无法在实体框架中使用级联删除。 I'm trying to automatically delete a row of "Players" when I delete one of "HighScoreListEntry", but even if the SQL Code looks fine it does only delete the row in the HighScoreListEntry table. 我尝试删除“ HighScoreListEntry”之一时自动删除“播放器”行,但是即使SQL代码看起来正常,它也只会删除HighScoreListEntry表中的行。

public class Player
{
    //Navigation Property
    public List<HighScoreListEntry> HSLEs { get; set; }

    //Properties
    public int PlayerId { get; set; }
    public String FName { get; set; }
    public String LName { get;  set; }

    public Player(String fName, String lName)
    {
        FName = fName;
        LName = lName;
    }

    public Player()
    {

    }
}

public class HighScoreListEntry
{
    //Navigation Property
    [Required]
    public Player Player { get; set; }

    //Foreign key
    public int PlayerID { get; set; }

    //Properties
    [Key]
    public int HSLEId { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTime Time { get; set; }
    public int Value { get; set; }

    public HighScoreListEntry(Player player, TimeSpan duration, DateTime time, int value)
    {
        if (value < 0) throw new FormatException();
        Player = player;
        Duration = duration;
        Time = time;
        Value = value;
    }

    public HighScoreListEntry()
    {

    }
}

HighScoreListEntry DDL Code HighScoreListEntry DDL代码

    CREATE TABLE [dbo].[HighScoreListEntries] (
    [HSLEId]   INT      IDENTITY (1, 1) NOT NULL,
    [PlayerID] INT      NOT NULL,
    [Duration] TIME (7) NOT NULL,
    [Time]     DATETIME NOT NULL,
    [Value]    INT      NOT NULL,
    CONSTRAINT [PK_dbo.HighScoreListEntries] PRIMARY KEY CLUSTERED ([HSLEId] ASC),
    CONSTRAINT [FK_dbo.HighScoreListEntries_dbo.Players_PlayerID] FOREIGN KEY ([PlayerID]) REFERENCES [dbo].[Players] ([PlayerId]) ON DELETE CASCADE
);


GO
CREATE NONCLUSTERED INDEX [IX_PlayerID]
    ON [dbo].[HighScoreListEntries]([PlayerID] ASC);

I know the method name isn't the best but that's not the problem 我知道方法名称不是最好的,但这不是问题

    public bool edit(HighScoreListEntry entryOld, HighScoreListEntry entryNew)
    {
        try
        {
            db.HSLE.Remove(entryOld);
            db.HSLE.Add(entryNew);
            db.SaveChanges();
        } catch (Exception f)
        {
            Console.WriteLine(f.ToString());
            return false;
        }
        return true;
    }

It should not work the way you expected. 它不应该按您预期的方式工作。

When you remove HighScoreListEntry , no Player entity is removed as it is parent one. 当您删除HighScoreListEntry ,不会删除任何Player实体,因为它是父实体。 If you were removing an Player all HighScoreListEntry entities belonging to the removed player would be deleted as well. 如果要删除Player属于删除的Player所有HighScoreListEntry实体也将被删除。

See related question for reference - How do I use cascade delete with SQL Server? 请参阅相关问题以供参考- 如何在SQL Server中使用级联删除?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM