1

enter image description here

Here is my code:

var stacy = context.People.Add(new People(1, "Stacy", "Bateman", new DateTime(1992, 3, 12)));
var matthew = context.People.Add(new People(1, "Matthew", "Gordon", new DateTime(1992, 3, 12)));
var adam = context.People.Add(new People(1, "Adam", "Herd", new DateTime(1992, 3, 12)));

Ensemble ensemble = new Ensemble();
ensemble.name = "Little River Band";
ensemble.EnsembleMembers = new List<People>();
ensemble.EnsembleMembers.Add(adam);
ensemble.EnsembleMembers.Add(stacy);
context.Ensemble.Add(ensemble);
context.SaveChanges();

As you can probably tell I have two tables (People & Ensemble) which are connected via a one to many relationship wherein an ensemble can have many people but a single person can only be apart of one ensemble.

My question is say if I wanted to remove the person stacy from the ensemble 'Little River Band' how would I do this while still retaining stacys other information e.g phone, email, firstname, surname.

4

1 回答 1

1

您将不得不使用流利的 API 来设置关系,并且我认为将 OnCascadeDelete 设置为 false。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

modelBuilder.Entity<Ensemble>()
.HasMany(a => a.EnsembleMembers)
.WithOptional().WillCascadeOnDelete(false);
}
于 2013-10-19T11:49:14.987 回答