C# EntityState.Added在特定情况下无效

C# EntityState.Added在特定情况下无效,c#,asp.net,entity-framework,entity,C#,Asp.net,Entity Framework,Entity,从昨天开始,我一直在讨论一个关于实体的问题,该实体似乎在特定情况下保存更改后未添加 我有一个table Person和一个包含外键PersonId的table History。 因此,my Person实体具有由EF生成的Histories属性,该属性是历史的ICollection 我想问题可能很简单,但我就是想不出来 这是我的页面: public partial class MyPage { private MyService service; private Person p

从昨天开始,我一直在讨论一个关于实体的问题,该实体似乎在特定情况下保存更改后未添加

我有一个table Person和一个包含外键PersonId的table History。 因此,my Person实体具有由EF生成的Histories属性,该属性是历史的ICollection

我想问题可能很简单,但我就是想不出来

这是我的页面:

public partial class MyPage
{
    private MyService service;
    private Person person;

    protected void Page_Load(object sender, EventArgs e)
    {
        service = new MyService();
        person = service.GetCurrentPerson(Request.QueryString["id"]);
    }

    protected void _SomeButton_Click(object sender, EventArgs e)
    {
        Discipline.TypeE disc = something;
        service.Test(equine, disc);
    }
}
这是我的服务:

public class MyService
{
    private HistoryRepo historyRepo = new HistoryRepo();
    private PersonRepo personRepo = new PersonRepo();

    public Person GetCurrentPerson(string id)
    {
        //Returns the current person from the given id
    }

    public void Test(Person person, Discipline.LabelE disc)
    {
        using (var uof = new UnitOfWork())
        {
            historyRepo.AddOrUpdate(new History(person, History.ActionE.Competition));
            personRepo.AddOrUpdate(person);
            uof.Commit(); // just does a SaveChanges()

            int historiesCount = person.Histories.Count;
        }
    }
}
这是可行的,历史计数是递增的。 但是,如果我只添加这一行,这将不再有效:

    public void Test(Person person, Discipline.LabelE disc)
    {
        using (var uof = new UnitOfWork())
        {
            List<Person> someVariableINeed = personRepo.GetByCompetition(disc); // this line

            historyRepo.AddOrUpdate(new History(person, History.ActionE.Competition));
            personRepo.AddOrUpdate(person);
            uof.Commit();

            int historiesCount = person.Histories.Count;
        }
    }

1.工作单元和存储库需要共享同一个DbContext2实例。entry.State=EntityState.Added将添加整个层次结构:msdn.microsoft.com/en-us/magazine/dn166926.aspx谢谢你的回复科林,我用存储库代码编辑了第一篇文章。从理论上讲,它们确实共享相同的上下文实例;
public class PersonRepo : RepositoryBase<Person>
{
    public List<Person> GetByCompetition(Discipline.TypeE disc)
    {
        var result =
            from person in entities.Persons
            join comp in entities.Competitions
            on new { ident = person.Id, discipline = (int)disc } equals new { ident = comp.PersonId, discipline = comp.DisciplineId } into tuple
            select person;

        return result.ToList();
    }
}

public class RepositoryBase<TObject> where TObject : class, IEntity
{
    protected Entities entities
    {
        get { return UnitOfWork.Current.Context; }
    }
}

public class UnitOfWork : IDisposable
{
    private const string _httpContextKey = "_unitOfWork";
    private Entities _dbContext;

    public static UnitOfWork Current
    {
        get { return (UnitOfWork)HttpContext.Current.Items[_httpContextKey]; }
    }

    public UnitOfWork()
    {
        HttpContext.Current.Items[_httpContextKey] = this;
    }

    public void Commit()
    {
        _dbContext.SaveChanges();
    }
}