简体   繁体   English

实体框架不将更改提交到数据库

[英]Entity Framework does not commit changes to database

This is really odd. 这真的很奇怪。 I'm trying to a simple thing, I'm trying to find an entity, change a field of that entity, and then commit the changes to the database: 我正在尝试一件简单的事情,我正在寻找一个实体,更改该实体的字段,然后将更改提交到数据库:

using (AppDbContext context = new AppDbContext()) {
    try {
        var member = context.Members.SingleOrDefault(m = m.MembershipNumberId == membershipNo);
        var centre = context.Centres.SingleOrDefault(c => c.CentreId == oldCentreId);

        if (member != null) {
            if (centre != null) {
                member.Centre_id = Centre.id;
                context.SaveChanges();
            }                    
        }
    } catch (Exception e) {
        HandleError(e);
    }
}

So pretty straight forward stuff. 非常简单的东西。 However, for some reason no changes are being committed to the the database, and in fact, the EntityState is remaining Modified . 但是,由于某种原因,没有更改要提交给数据库,实际上, EntityState保持Modified

Here's where things get really weird in my view: I put a breakpoint in AppDbContext.SaveChanges() so I could look at the changetracker in debugging. 在我看来,这里的事情真的很奇怪:我在AppDbContext.SaveChanges()设置了一个断点,以便可以在调试时查看changetracker。

When I do this, everything looks good, I see my Centre entity in the Unchanged state, and I see my Member entity in the Modified state. 当我这样做时,一切看起来都很好,我看到了我的Centre实体处于Unchanged状态,并且看到了我的Member实体处于Modified状态。 When I open the Member entity that is in the ChangeTracker in the watch window, for some reason this makes the code work when I resume, and the database is updated accordingly. 当我在监视窗口中的ChangeTracker中打开Member实体时,由于某种原因,这使代码在我恢复时可以正常工作,并且相应地更新了数据库。

So long story short: SaveChanges doesn't commit changes to the database. 长话短说: SaveChanges不会将更改提交到数据库。 But, if I put a breakpoint in save changes, look at the changetracker in the watch window, and then open up the modifed entity ( Member in my example), then just resume, it does work! 但是,如果我在保存更改中设置了一个断点,请在监视窗口中查看changetracker,然后打开修改后的实体(在我的示例中为Member ),然后继续工作就可以了!

So what could be happening when you look at an entity in the changetracker in the watch window that is causing the code to work? 那么,当您在监视窗口中的changetracker中查看导致代码正常工作的实体时,会发生什么情况? I'm at a loss here and would greatly appreciate any help. 我在这里不知所措,非常感谢您的帮助。 Thanks in advance. 提前致谢。

I've worked out what it was. 我已经知道是什么了。 The Member POCO had the following field on it: POCO成员具有以下字段:

[Required] public virtual Account [必需]公共虚拟帐户

A required lazy loaded field. 必填的延迟加载字段。 It was spitting out a validation error because it thought there was no account, when I opened the entity in the watch window, however, it was forcing the Lazy loading to kick in, hence loading the Account. 它吐出了一个验证错误,因为当我在监视窗口中打开实体时,它认为没有帐户,但是,这迫使强制执行惰性加载,从而加载了帐户。 I've taken the [Required] off and now it's working. 我已经取消了[必需],现在它可以工作了。

Now to work out why I wasn't seeing the validation error at first :/ 现在找出为什么我最初没有看到验证错误的原因:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM