gpt4 book ai didi

c# - Entity Framework 代码优先级联删除一对多

转载 作者:行者123 更新时间:2023-11-30 14:06:39 25 4
gpt4 key购买 nike

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

使用我编写的代码,在尝试添加一个有 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, 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 类生成。但我不知道为什么?

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

enter image description here

肯定在 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);

最佳答案

流畅的关系配置

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

关于c# - Entity Framework 代码优先级联删除一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46402907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com