加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 实体框架代码第一级联删除一对多

发布时间:2020-12-15 23:40:48 所属栏目:百科 来源:网络整理
导读:我正在尝试创建一个小型演示解决方案来试验EF CF Cascading删除. 使用我编写的代码,我在尝试添加2辆车的人时遇到以下错误. 目的是增加一个人2辆车.然后删除该人并同时删除链接的汽车. System.InvalidCastException: Unable to cast object of type ‘System.
我正在尝试创建一个小型演示解决方案来试验EF CF Cascading删除.

使用我编写的代码,我在尝试添加2辆车的人时遇到以下错误.

目的是增加一个人2辆车.然后删除该人并同时删除链接的汽车.

System.InvalidCastException: Unable to cast object of type ‘System.Collections.Generic.List`1[EF_Cascading_Delete_Experiment.Car]’ to type ‘EF_Cascading_Delete_Experiment.Car’.

我正在尝试构建一个简单的示例,其中有一个带有汽车列表的人员

这是我的人物和汽车类:

public class Person
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Car> Cars { get; set; }
}
public class Car
{
    [Key]
    public int id { get; set; }
    public string CarName { get; set; }
}

这是我的简单代码,试图为一个有2辆车的人做广告:

public static void CarTest()
    {
        using (Model1 db = new Model1())
        {
            Person personToAdd = new Person();
            personToAdd.Name = "trev";
            personToAdd.Cars = new List<Car>();

            Car car1 = new Car
            {
                CarName = "Vectra"
            };

            Car car2 = new Car
            {
                CarName = "Focus"
            };

            personToAdd.Cars.Add(car1);
            personToAdd.Cars.Add(car2);

            db.Person.Add(personToAdd);

            db.SaveChanges();
        }
    }

错误发生在该行

db.Person.Add(personToAdd);

这是我的DbContext:

public class Model1 : DbContext
{
    // Your context has been configured to use a 'Model1' connection string from your application's 
    // configuration file (App.config or Web.config). By default,this connection string targets the 
    // 'EF_Cascading_Delete_Experiment.Model1' database on your LocalDb instance. 
    // 
    // If you wish to target a different database and/or database provider,modify the 'Model1' 
    // connection string in the application configuration file.
    public Model1()
        : base("name=Model1")
    {
    }

    // Add a DbSet for each entity type that you want to include in your model. For more information 
    // on configuring and using a Code First model,see http://go.microsoft.com/fwlink/?LinkId=390109.

    public virtual DbSet<Person> Person { get; set; }
    public virtual DbSet<Car> Car { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>()
            .HasOptional(a => a.Cars)
            .WithOptionalDependent()
            .WillCascadeOnDelete(true);
    }
}

EF生成的迁移代码如下所示:

public partial class addedbackeverythingincludingcascadingdelete : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Cars",c => new
                {
                    id = c.Int(nullable: false,identity: true),CarName = c.String(),})
            .PrimaryKey(t => t.id);

        CreateTable(
            "dbo.People",c => new
                {
                    Id = c.Int(nullable: false,Name = c.String(),Cars_id = c.Int(),})
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Cars",t => t.Cars_id,cascadeDelete: true)
            .Index(t => t.Cars_id);

    }

    public override void Down()
    {
        DropForeignKey("dbo.People","Cars_id","dbo.Cars");
        DropIndex("dbo.People",new[] { "Cars_id" });
        DropTable("dbo.People");
        DropTable("dbo.Cars");
    }
}

对我来说,看起来迁移代码不正确?哪个会根据我的人物生成而生成汽车课.但我无法解释为什么?

当我查看数据库中的表时,它们看起来是错误的.

enter image description here

当然在Car表中应该有一个PersonId?在Person表中不是CarId?

解:

非常感谢伊万,这是我的解决方案.我把它放在这里所以我可以将他的问题标记为答案.

我的课程现在看起来像这样:

public class Person
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<Car> Cars { get; set; }
}
public class Car
{
    [Key]
    public int id { get; set; }
    public string CarName { get; set; }
}

在测试时,即使Ivan说我不需要它,我发现级联删除不起作用,除非我保留这段代码:

modelBuilder.Entity<Person>()
            .HasMany(a => a.Cars)
            .WithOptional() // or `WithRequired() in case Car requires Person
            .WillCascadeOnDelete(true);

解决方法

流畅的关系配置

modelBuilder.Entity<Person>()
    .HasOptional(a => a.Cars)
    .WithOptionalDependent()
    .WillCascadeOnDelete(true);

是错的. HasOptional,HasRequired,WithOptionalDependent,WithOptionalPrincipal等是一对一的关系,而你有一对多的关系.

正确的配置如下:

modelBuilder.Entity<Person>()
    .HasMany(a => a.Cars)
    .WithOptional() // or `WithRequired() in case Car requires Person
    .WillCascadeOnDelete(true);

现在迁移应该如下所示:

CreateTable(
    "dbo.People",c => new
        {
            Id = c.Int(nullable: false,})
    .PrimaryKey(t => t.Id);

CreateTable(
    "dbo.Cars",c => new
        {
            id = c.Int(nullable: false,Person_Id = c.Int(),})
    .PrimaryKey(t => t.id)
    .ForeignKey("dbo.People",t => t.Person_Id,cascadeDelete: true)
    .Index(t => t.Person_Id);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读