EF CFカスケード削除を試す小さなデモソリューションを作成しようとしています。エンティティフレームワークコード最初にカスケードする1つを複数に削除
私が書いたコードでは、2台の車でPersonを追加しようとすると次のエラーが発生します。
2台の車を追加することを目的としています。その人を削除し、リンクされた車は同時に削除されます。
System.InvalidCastExceptionの:型のオブジェクトをキャストすることができません 'System.Collections.Generic.List`1は[EF_Cascading_Delete_Experiment.Car]' '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です:EFによって生成
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);
}
}
移行コードは次のようになります。私には
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クラスに基づいて生成されたものです。しかし、私はなぜうまくいかないの?
データベースのテーブルを見ると、それらは間違っています。
確かに車のテーブルに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; }
}
すると、テスト、イワンが言うにもかかわらず、私はこのコードを保持しない限り、私は動作しないでしょうカスケードを削除見つかり、それを必要はありません
modelBuilder.Entity<Person>()
.HasMany(a => a.Cars)
.WithOptional() // or `WithRequired() in case Car requires Person
.WillCascadeOnDelete(true);
は、仮想キーワードで試してみてください。例:public virtual ICollection Cars {get;セット; } 'publicリストの代わりに' 車{get;セット; } ' –
@AmitKumarと同じエラーです。 –
1人で車がたくさんある場合、あなたの関係は間違って設定されます。車にはPerson_Id FKが必要です。あなたのリンクされたイメージを見てみましょう。(1対多カーの)関係ではなく、(多くの対1対1)関係を示します。 – Flater