简体   繁体   English

了解使用流利的 api 禁用级联删除

[英]Understanding disabling on cascade delete using fluent api

I am new to asp.net and I am following a course where we added this piece of code to disable oncascade delete from related table using the below code:我是 asp.net 的新手,我正在学习使用以下代码添加这段代码以禁用相关表中的级联删除的课程:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Attendance>()
        .HasRequired(a => a.Gig)
        .WithMany()
        .WillCascadeOnDelete(false);
        
    base.OnModelCreating(modelBuilder);
}

I want to understand many parts:我想了解很多部分:

  1. why we define entity method with attendance type,based on my understanding this type should be used when creating the method to specify the returned values not when calling the method.为什么我们用出勤类型定义实体方法,根据我的理解,应该在创建方法时使用这种类型来指定返回值,而不是在调用方法时。

  2. what does HasRequired mean and what does withmany means? HasRequired 是什么意思, HasRequired是什么意思?

On cascade delete级联删除

Let's say there is another type link to your attendance.假设您的出席有另一种类型的链接。 So in database this other type (table) will have a foreign key to the Attendance table.因此,在数据库中,这种其他类型(表)将具有Attendance表的外键。

If you try to delete an Attendance record in the datatable, it will fail if this record is used in the other table.如果您尝试删除数据表中的一条Attendance记录,如果该记录在另一个表中使用,它将失败。

With the cascade delete it will delete all related records besides the Attendance record.级联删除将删除除考勤记录之外的所有相关记录。

If the cascade delete is not enable the deletion will fail because other records need this Attendance record.如果不启用级联删除,删除将失败,因为其他记录需要这条考勤记录。

Entity实体

No matter if you generate the database from your code or if the database already exists, your C# class representing the table cannot provide all information (for example if you do not follow the convention, EF cannot know which field is the primary key, which field is not nullable (see next point), or if the table name will match the class name).不管你是从你的代码生成数据库还是数据库已经存在,你的C# class 代表表都不能提供所有信息(例如如果你不遵循约定,EF就无法知道哪个字段是主键,哪个字段不可为空(请参阅下一点),或者表名称是否与 class 名称匹配)。

With the modelBuilder.Entity<Attendance>() you will help EF to build his "inner model of how the database will looks like".使用modelBuilder.Entity<Attendance>()您将帮助 EF 构建他的“数据库外观的内部 model”。 In this case you provide to EF additional information about the "Attendance" type.在这种情况下,您向 EF 提供有关“出勤”类型的附加信息。

So you could warn EF this table is in another schema or the table name is different...所以你可以警告 EF 这个表在另一个模式中或者表名不同......

Potentially you could do that for all your entities/tables.您可能可以为所有实体/表执行此操作。

IsRequired (not used in your example but I confond IsRequired and HasRequired so I let the explanation here) IsRequired (在您的示例中未使用,但我对IsRequiredHasRequired有信心,所以我在这里解释)

The IsRequired is used with nullable types. IsRequired与可为空的类型一起使用。 In database you can have a type "varchar(x) not null" and in C# it will be a "string".在数据库中,您可以有一个类型“varchar(x) not null”,在 C# 中,它将是一个“字符串”。 But string can be nullable in C# so the HasRequired is a configuration that will tell your ORM "Yes the type is nullable but in my configuration it should not be the case".但是字符串在 C# 中可以为空,因此HasRequired是一种配置,它会告诉您的 ORM “是的,该类型可以为空,但在我的配置中不应该是这种情况”。

So when EF will generate the database the type will be "not null" or when your EF will validate the entity before calling the database, that check will be done.因此,当 EF 将生成数据库时,类型将为“非空”,或者当您的 EF 将在调用数据库之前验证实体时,将完成该检查。

HasRequired有要求

It's the same concept as the IsRequired but a little more complex since it's used when there is multiple tables involved.它与IsRequired的概念相同,但更复杂一些,因为它在涉及多个表时使用。 For example Contract and Customer, a contract is owned by one customer but a customer can have multiple contracts.例如合同和客户,一份合同归一个客户所有,但一个客户可以有多个合同。 So in the database Contract table will have a foreign key to Customer and in the class Contract will have a (navigation) property named Customer.因此,在数据库合同表中将有一个指向客户的外键,并且在 class 合同中将有一个名为客户的(导航)属性。

public class Contract 
{
    public Customer Customer { get; set; }
}

public class Customer
{       
}

modelBuilder.Entity<Contract>()
    .HasRequired(c => c.Customer)

This configuration will indicate Contract have a navigation property to Customer and this is a required one meaning the foreign key cannot be null.此配置将指示合同对客户具有导航属性,这是必需的,这意味着外键不能是 null。 If you want it to be nullable you should use HasOptional .如果您希望它可以为空,您应该使用HasOptional

WithMany Now you configured the HasRequired, you have the WithMany. WithMany现在你配置了 HasRequired,你就有了 WithMany。 This one will indicate you can have multiple Contract for a Customer (Contract has a required Customer who have many Contracts: It's a "one to many" relationship).这将表明您可以为一个客户拥有多个合同(合同有一个拥有许多合同的必需客户:这是一个“一对多”的关系)。 But it also indicates the Customer does not care about the contracts:但这也表明客户不关心合同:

public class Customer
{       
}

Here you can see the Customer class does not have a (navigation) property to the contracts.在这里,您可以看到客户 class 没有合同的(导航)属性。 If you don't need the Customer object to be able to reach the contracts this is perfectly fine.如果您不需要客户 object 来达成合同,这很好。

Now if you want to have that ability you will need to do this:现在,如果你想拥有这种能力,你需要这样做:

public class Customer
{
   public List<Contract> Contracts { get; set; }       
}

modelBuilder.Entity<Contract>()
    .HasRequired(c => c.Customer)
    .WithMany(cu => cu.Contracts)

In that case you indicate Contract have a relationship required to Customer and this relation from the Customer object point of view lead to "Contracts" property.在这种情况下,您表明合同与客户有关系,并且从客户 object 的角度来看,这种关系导致“合同”属性。

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

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