简体   繁体   English

EF Fluent API C#级联删除问题

[英]EF fluent API c# cascade delete issue

I am having problems configuring a model to not cascade delete. 我在配置模型以不进行级联删除时遇到问题。

The modeL. 该模型。

public class Hit
{
    public int Id { get; set; }

    // Foreign Key
    public int AccountId { get; set; }

    // Foreign Key
    public int LeadId { get; set; }

    //navigation properties
    [ForeignKey("LeadId")]
    public Lead lead { get; set; }

    //navigation properties
    [ForeignKey("AccountId")]
    public Account account { get; set; }

}

Fluent API: 流利的API:

 modelBuilder.Entity<Hit>()
          .HasRequired(t => t.lead)
          .WithMany()
          .HasForeignKey(t => t.LeadId)
          .WillCascadeOnDelete(false);

When I try to migrate it, i get this in create table on Hit: 当我尝试迁移它时,我会在Hit的create table中得到它:

.ForeignKey("dbo.Accounts", t => t.AccountId, cascadeDelete: true)
.ForeignKey("dbo.Leads", t => t.Lead_Id)
.ForeignKey("dbo.Leads", t => t.LeadId, cascadeDelete: true)

What I want to achieve for both leads and accounts is this in the migration script: 我想要为潜在客户和客户实现的目标是在迁移脚本中实现以下目标:

.ForeignKey("dbo.Leads", t => t.LeadId)

How should I go about doing this? 我应该怎么做呢? Its prbably easy, but I am new to all this:( 这很容易,但是我对这一切还是陌生的:(

EDIT 1: 编辑1:

I added this to the fluent API: 我将此添加到了流畅的API:

 modelBuilder.Entity<Lead>().HasMany(i => i.Hits).WithOptional().HasForeignKey(s=> s.LeadId).WillCascadeOnDelete(false);

Which remove cascadelete from: .ForeignKey("dbo.Leads", t => t.LeadId) 从以下代码中删除层叠串:.ForeignKey(“ dbo.Leads”,t => t.LeadId)

However, I also want to remove it from: 但是,我也想从以下位置删除它:

 .ForeignKey("dbo.Accounts", t => t.AccountId, cascadeDelete: true)

The easy solution would be to remove it from the migration script. 一种简单的解决方案是将其从迁移脚本中删除。 But I would like how to do this with FLuent API or other methods. 但是我想如何使用FLuent API或其他方法来做到这一点。

Any ideas? 有任何想法吗?

Like I already said in the comments above, if you use Fluent API for mapping your FKs remove the [ForeignKey] attributes from your navigation properties. 就像我在上面的评论中已经说过的,如果您使用Fluent API映射FK,请从导航属性中删除[ForeignKey]属性。

You can then do non-cascading mappings like this: 然后,您可以像下面这样进行非级联映射:

modelBuilder.Entity<Hit>().HasOptional(m => m.account).WithMany().HasForeignKey(m
=> m.AccountId).WillCascadeOnDelete(false);

Finally you have to check if your FK columns should be optional or required, for optional mappings change your FK properties AccountId and LeadId to data type int? 最后,您必须检查FK列是否应该是可选的或必需的,因为可选映射将FK属性AccountIdLeadId更改为数据类型int? , otherwise you have to change the HasOptional() part of your FK mappings to HasRequired() . ,否则您必须将FK映射的HasOptional()部分更改为HasRequired()

Define your model like so: 像这样定义模型:

public class Hit
{
    public int Id { get; set; }

    public virtual Lead Lead { get; set; }

    public virtual Account Account { get; set; }
}

The virtual keyword will let Entity Framework know you are defining a foreign key and will match on Id automatically. virtual关键字将使Entity Framework知道您正在定义外键,并将自动在ID上进行匹配。

Remove the HasRequired(t => t.lead).WithMany() part from Fluent API. 从Fluent API中删除HasRequired(t => t.lead).WithMany()部分。 If a property is required it cannot exist without it and will cascade on delete. 如果需要一个属性,那么没有它就无法存在,并且在删除时会级联。

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

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