我在《实体框架4.1》中提出了将儿童纳入一个根本实体的问题。
考虑到以下基础设施实体基类和两个组织:
public abstract class EntityBase<TKeyDataType>
{
[Key]
public TKeyDataType Id { get; set; }
// Equality methods ommitted for brevity...
}
public class Foo : EntityBase<int>, IAggregateRoot
{
public string Foo1 { get; set; }
public virtual ICollection<FooSibling> Siblings { get; set; }
}
public class FooSibling : EntityBase<int>
{
public string SiblingPropFoo { get; set; }
public int FooId { get; set; }
public Foo Foo { get; set; }
}
通知:IAggregateRoot
(调整空接口——在“数据”背景下将其视作元数据)。
迄今为止,情况良好。 如果我这样做的话,欧洲复兴开发银行就建立了适当的1:many关系数据库。
我在这两个实体上的唯一简图是:
modelBuilder.Entity<Foo>()
.HasMany(x => x.Siblings)
.WithRequired(x=>x.Foo)
.WillCascadeOnDelete(true);
Foo
。 贴上
问题是,在Foo POCO中增加FooSiblings POCO时,我不得不使用这一服务方法中显示的独特负面数字:
public ResponseBase UpdateBy(RequestBase<Foo> request)
{
ResponseBase response = new ResponseBase();
try
{
Foo foo = FooRepository.FirstOrDefault(x => x.Id == request.Entity.Id);
// Dummy adds to test associations.
// These come back on the Foo inside the request, but I m explicitly putting them here
// for the purpose of this question.
request.Entity.Siblings.Add(new FooSibling() { Id = -2, SiblingPropFoo = "Prop1", SiblingPropFoo2 = "Prop2" });
request.Entity.Siblings.Add(new FooSibling() { Id = -1, SiblingPropFoo = "Prop1", SiblingPropFoo2 = "Prop2" });
// Update Foo s scalars and children (mapping is Foo->Foo)
foo = AutoMapper.Mapper.Map(request.Entity, foo);
UnitOfWork.Commit();
response.Success = true;
}
catch (Exception e)
{
response.Success = false;
response.Message = e.Message;
}
return response;
}
一旦打电话到<代码>UnitofWork.Commit(SaveChanges,此处不作魔法,所有方面都很好......
然而,如果我不像现在这样使用独特的负面数字,而只是试图把自己的母子放在一边,就象这样:
request.Entity.Siblings.Add(new FooSibling() { Foo = foo, SiblingPropFoo = "Prop1", SiblingPropFoo2 = "Prop2" });
request.Entity.Siblings.Add(new FooSibling() { Foo = foo, SiblingPropFoo = "Prop1", SiblingPropFoo2 = "Prop2" });
Only one gets persisted to the database.
The only other way I know of to do it without using negative numbers is to use the FooSiblings DbSet directly in the service method:
IRepository<FooSibling> siblingRepo = new CookieCutterEntityFrameworkRepository<FooSibling>(UnitOfWork);
siblingRepo.Insert(new FooSibling() { FooId = foo, .... });
我的库奇特存放处正在把所有的德贝施特 st等地 abstract。
但是,......为了澄清起见,排除所有抽象和通用的voodoo,问题真的在于,能否更新我的Foo POCO(原实体),通过一个DbSet增加新的兄弟姐妹,而没有使用负面数字?
For reference (without abstraction using pure DbContext):
// This works (using multiple DbSets/Repositories always make life easier...)
Ctx.Foos.Update(foo);
Ctx.FooSiblings.Add(new FooSibling() { Foo = foo, ... });
Ctx.FooSiblings.Add(new FooSibling() { Foo = foo, ... });
Ctx.SaveChanges();
// This works too (using negative number trick - foo scalar properties get
// updated and the siblings get persisted to the database properly).
foo.Siblings.Add(new FooSibling() { Id = -2, ....});
foo.Siblings.Add(new FooSibling() { Id = -1, ....});
Ctx.Foos.Update(foo);
Ctx.SaveChanges();
// This doesn t work (but it s what I m striving for to drive everything off the root).
foo.Siblings.Add(new FooSibling() { Foo = foo });
foo.Siblings.Add(new FooSibling() { Foo = foo });
Ctx.Foos.Update(foo);
Ctx.SaveChanges();
在最后一例(非工作案件)中,Im试图以引起对Foo POCO本身的任何变化的方式来加以配置。
I ve tried with Proxies turned off and on. Also, in the way this is setup, the context remains in scope for the life of the entire HTTP request.
如果不可能,你会提出什么建议?