English 中文(简体)
《实体框架法》第一编——通过综合体增加1:
原标题:Entity Framework Code First - Adding 1:many through aggregate root

我在《实体框架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);

no a 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.

如果不可能,你会提出什么建议?

最佳回答

如果我正确读懂,问题可望通过斜体或正文。

我想谈谈你的最后一套例子。 当你附上图表时,图表中的所有内容都作为不可改动的附后。 期间 这里的情况表明,在纽约市了解他们的情况之前,你如何将“儿童”带入或附属于根基(如oo的兄弟姐妹)。 如果该 f为背景所熟知的,并且是你们的捍卫者,那么就了解了你的联系。 即使根基不是新的。 否则,你就不得不将其国家改为“不改变”。 但就你的情况而言,你正在拿着一张不相干的图表,因此,图表中的每一项都被视为没有变化。 你们必须明确改变每个兄弟姐妹的状况,以适应你们想要的“拯救儿童”组织需要的信息。

现在请您存放。 我对负面数字没有想法。 我没有时间对此进行试验。 (确保这一节点如何渗透储蓄,但会 s:) 这取决于你们的oo托存问题发生的情况。 如果按照情况追踪这一已返回的oo,那么我将expect:当您的荒漠化、土地退化和干旱的兄弟姐妹时,环境将知道他们,并将形成一个插入物。 但是,如果它没有被跟踪,那么当你们是捍卫民主者协会的兄弟时,就不知道“捍卫民主力量”的情况。 无论在什么时候,你都把图表(foo + 'siblings)重新编号为“UNchanged”,以便你重新获得所希望的插入。

这完全取决于在贵岛存放处所处位置上的任何东西,以及如果有任何影响,谁会影响到汽车。 我猜测,在了解图象之前,你正在做所有工作,我认为你再说一遍,这不会导致国家公务员系统被送去。

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签