EntityState.Added在特定情况下无效

时间:2015-05-20 09:33:28

标签: c# asp.net entity-framework entity

从昨天开始,我一直被困在一个关于在特定情况下SaveChanges()之后似乎没有添加的实体的问题。

我有一个表Person和一个表History,它有一个外键PersonId。 因此,我的Person实体有一个由EF生成的Histories属性,它是History的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;
        }
    }
}

这有效,historiesCount递增。 但是,如果我只是添加此行,则此功能不再起作用:

    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;
        }
    }

someVariableINeed甚至没有使用,但person.Histories不再增加。 如果我刷新它的页面,但是在uof.Commit()之后它不是,我需要它在这里...

我检查过,在这两种情况下,AddOrUpdate只是执行此操作:entry.State = EntityState.Added;

我不认为personRepo.GetByCompetition可能是问题本身,因为用Person someOtherVariable = personRepo.Get(person.Id);替换它(例如)并不会使它更好用。

但是,它适用于List<Competition> someOtherVariable = inscriptionRepo.GetByDiscipline(disc);等其他示例。

我真的不知道问题出在哪里。 如果有人有想法请随意,如果需要,我可以添加其他代码。

非常感谢。

修改

存储库:

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();
    }
}

0 个答案:

没有答案