【问题标题】:Entity Framework Code First Cascading Delete One To Many实体框架代码优先级联删除一对多
【发布时间】:2017-09-25 10:22:37
【问题描述】:

我正在尝试创建一个小型演示解决方案来试验 EF CF 级联删除。

使用我编写的代码,尝试添加一个有 2 辆车的人时出现以下错误。

目标是添加一个有 2 辆车的人。然后删除人员,同时删除关联的汽车。

System.InvalidCastException:无法将“System.Collections.Generic.List`1[EF_Cascading_Delete_Experiment.Car]”类型的对象转换为“EF_Cascading_Delete_Experiment.Car”类型。

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

这是我的 Person & 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, identity: true),
                    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");
    }
}

在我看来,迁移代码似乎不正确?这将根据我的 Person & Car 类生成。但我不知道为什么?

当我查看数据库中的表时,它们看起来不对。

在 Car 表中肯定应该有一个 PersonId 吗?在 Person 表中不是 CarId?

解决方案:

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

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

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);

【问题讨论】:

  • 尝试使用虚拟关键字。例如 public virtual ICollection&lt;Car&gt; Cars { get; set; } 而不是 public List&lt;Car&gt; Cars { get; set; }
  • @AmitKumar 同样的错误。
  • 如果一个人可以拥有多辆汽车,那么你们的关系设置错误。 Car 应该有一个 Person_Id FK,而不是相反。查看您的链接图像,它显示的是(多人对一辆车)关系,而不是(一人对多车)关系。

标签: c# entity-framework entity-framework-migrations


【解决方案1】:

流畅的关系配置

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

错了。 HasOptionalHasRequiredWithOptionalDependentWithOptionalPrincipal 等用于one-to-one 关系,而您拥有one-to-many

正确的配置如下:

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, identity: true),
            Name = c.String(),
        })
    .PrimaryKey(t => t.Id);

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

【讨论】:

  • 还有 - 当我现在查看人和汽车之间的关系时,它说删除和更新规则是“无操作”
  • 这是代表one-to-many 的可能有效 EF 模型之一 - 在one 侧具有集合导航属性。您还可以在许多方面添加参考导航和/或 FK 属性,但这不是必需
  • 您应该在Cars 表中看到Person_Id 列,FK 为People。当然是在迁移数据库之后。
【解决方案2】:

实际上,您还没有在汽车模型类中添加 Person 属性。 你的模型应该是这样的。

请看这里。 Cascade delete in one to many relationship

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

现在运行迁移。并对其进行测试。

【讨论】:

    【解决方案3】:

    首先,我认为最好在 Person 类中添加 virtual 关键字(用于延迟加载):

    public virtual List<Car> Cars { get; set; }
    

    您可以使用所需的注释在汽车类上添加对 Person 的引用:

    [Required]
    public virtual Person Person { get; set; }
    

    由于需要一个人,删除一个人将级联删除他所有的汽车。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2016-06-18
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      相关资源
      最近更新 更多