Unable to delete child objects from POCO using Unit Of Work template

I use the POCO classes in an EF4 CTP5 project, and I'm having trouble deleting child properties. Here is my example (hopefully not too long).

Matching Tour Class Parts

public partial class Tour
{
  public Guid TourId { get; private set; }
  protected virtual List<Agent> _agents { get; set; }

  public void AddAgent(Agent agent)
  {
    _agents.Add(agent);
  }

  public void RemoveAgent(Guid agentId)
  {
    var a = Agents.Single(x => x.AgentId == agentId);
    _agents.Remove(Agents.Single(x => x.AgentId == agentId));
  }
}

Command handler

public class DeleteAgentCommandHandler : ICommandHandler<DeleteAgentCommand>
{
  private readonly IRepository<Core.Domain.Tour> _repository;
  private readonly IUnitOfWork _unitOfWork;

  public DeleteAgentCommandHandler(
      IRepository<Core.Domain.Tour> repository, 
      IUnitOfWork unitOfWork
    )
  {
    _repository = repository;
    _unitOfWork = unitOfWork;
  }

  public void Receive(DeleteAgentCommand command)
  {
    var tour = _repository.GetById(command.TourId);
    tour.RemoveAgent(command.AgentId);

    // The following line just ends up calling
    // DbContext.SaveChanges(); on the current context.

    _unitOfWork.Commit();
  }
}

Here's the error I get when my UnitOfWork calls DbContext.SaveChanges()

: , NULL. , . , , , .

, EF , Tour.

dbContext.Agents.DeleteObject(a);, , dbContext POCO.

?

+3
2

, DeleteAgentCommandHandler (IRepository<Core.Domain.Agent>, ), - Delete(command.AgentId) .

IUnitOfWork factory, , T CreateRepository<T>(), . ( IUnitOfWork DeleteAgentCommandHandler, .)

/ . Agent Tour, . ITourRepository .

+1

, . , , :

http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/58a31f34-9d2c-498d-aff3-fc96988a3ddc/

(-, , ), DbContext OnModelCreating :

modelBuilder.Entity<Agent>()
.HasKey(AgentId)
.HasKey(TourId);

, EDMX , XAML, , . , , EDMX EF , , , , , , .

, , , , , . , XAML , .

0

All Articles