简体   繁体   English

级联删除和Entity Framework中的可选记录

[英]Cascade delete with optional records in Entity Framework

I'm trying to develop an ASP.net MVC application with Entity Framework and in last about 8 hours, i'm trying to fix something without a success. 我正在尝试使用Entity Framework开发ASP.net MVC应用程序,并且在最后大约8个小时内,我试图修复某些问题而没有成功。 I have 3 models, Overhour , Accounting and Vacation . 我有3个模型,“ Overhour ,“ Accounting和“ Vacation Overhour is the parent model, once we create an Overhour , we can create an Accounting or Vacation record with it for one time. Overhour是父模型,一旦创建了Overhour ,就可以一次创建一个AccountingVacation记录。 We don't need to create one, so we can have an Overhour without an Accounting or Vacation . 我们不需要创建一个,因此我们可以在没有AccountingVacation情况下进行Overhour Important thing is, when we delete an Overhour , we should delete both Accountings and Vacations (if they exist). 重要的是,当我们删除Overhour ,应该同时删除AccountingsVacations (如果存在)。

I'm using code first, i tried it with Data Annotations but it seems when you can't do that with optional models so i'm using Fluent Api now. 我首先使用代码,我使用数据注释进行了尝试,但是似乎您无法使用可选模型来做到这一点,因此现在使用Fluent Api。 Here is the thing i'm using for relation between an Overhour and Accounting : 这是我用于OverhourAccounting之间的关系的东西:

modelBuilder.Entity<Overhour>()
.HasOptional<Accounting>(s => s.Accounting)
.WithOptionalPrincipal()
.WillCascadeOnDelete(true);

Even if i use CodeFirst, i use a plugin to create model diagrams for testing purposes and model looks fine too (i guess): 即使我使用CodeFirst,我也使用插件创建用于测试目的的模型图,并且模型看起来也很好(我猜):

Diagram (it seems i can't include the image directly) (似乎我不能直接包含图像)

As i understand, it says "Cascade on End1Delete". 据我了解,它说“ Cascade on End1Delete”。 So when i delete an Overhour , accounting record should gets deleted too, but it's not working, it just sets it to null. 因此,当我删除Overhour ,会计记录也应被删除,但它不起作用,它只是将其设置为null。

Another thing is, i'm not sure which one is the parent. 另一件事是,我不确定哪个是父母。 I mean, should i have an Overhour model like this: 我的意思是,我应该有一个这样的Overhour模式Overhour

class Overhour {
    ...
    public int AccountingId { get; set; }
    public virtual Accounting Accounting { get; set; }
}

Or something like this: 或类似这样的东西:

class Overhour {
    ...
    public virtual Accounting Accounting { get; set; }
}

Or maybe i should include this properties to Accounting model? 或者,也许我应该将此属性包括到“会计”模型中? Like this: 像这样:

class Accounting {
    ...
    public virtual Overhour Overhour { get; set; }
}

Maybe i need to do both? 也许我需要两者都做?

I'm so confused, any ideas? 我很困惑,有什么想法吗?

Thanks 谢谢

I was able to get cascade delete with optional records to work using this: 我能够使用可选记录进行级联删除,以使用此记录进行工作:

public class Overhour
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual Guid Id { get; set; }
}

public class Accounting
{
    [ForeignKey("Overhour")]
    public virtual Guid Id { get; set; }

    public virtual Overhour Overhour { get; set; }
}

public class Vacation
{
    [ForeignKey("Overhour")]
    public virtual Guid Id { get; set; }

    public virtual Overhour Overhour { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Accounting>()
        .HasRequired(accounting => accounting.Overhour)
        .WithOptional()
        .WillCascadeOnDelete(true);

    modelBuilder.Entity<Vacation>()
        .HasRequired(vacation => vacation.Overhour)
        .WithOptional()
        .WillCascadeOnDelete(true);
}

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

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