实体框架4.1 RC(代码第一次) - 实体没有更新过协会 [英] Entity Framework 4.1 RC (Code First) - Entity not updating over association

查看:131
本文介绍了实体框架4.1 RC(代码第一次) - 实体没有更新过协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的是相当简单的。我有两个类:

What I'm trying to do is fairly simple. I have two classes:

public class TownRecord
    {
        public int Id { get; set; }
        public string ShortName { get; set; }
        public string FileName { get; set; }
        public string tags { get; set; }
        public virtual TownRecordType RecordType { get; set; }
        public DateTime? DateScanned { get; set; }
        public DateTime? RecordDate { get; set; }
        [StringLength(4000)]
        public string Comments { get; set; }
        public string UploadedBy { get; set; }
    }

    public class TownRecordType
        {
            public int Id { get; set; }
            public string RecordType { get; set; }
            public virtual ICollection<TownRecord> TownRecords {get; set; }
        }

当我想在TownRecord类更新记录类型的财产,我觉得该协会无法更新。不会抛出异常,但不执行更新:

When I want to update the RecordType property on the TownRecord class, I find that the association fails to update. No exception is thrown but the update is not performed:

 [HttpPost]
 public ActionResult Edit(int id, TownRecord tr, FormCollection collection)
 {
  TownRecordType newRecType = _ctx.TownRecordTypes.Find(Int32.Parse(collection["RecordType"]));
  tr.RecordType = newRecType;
  _ctx.Entry(tr).State = EntityState.Modified;
  _ctx.SaveChanges();
   return RedirectToAction("List");
  }

请注意:我打消了我的错误处理,清晰...

NOTE: I removed my error handling for clarity...

我见过类似这样的这里但我没有得到它。这可能是一个非常愚蠢的新手的错误,但我已经StackOverflowing和谷歌上搜索了几个小时还是一无所获。任何帮助是极大的赞赏。

I've seen a question similar to this here but I'm not getting it. This is probably a really foolish rookie mistake but I've StackOverflowing and Googling for several hours and getting nowhere. Any help is greatly appreciated.

推荐答案

由于您使用的是独立协会这是行不通的。 TownRecord TownRecordType 未镇记录的条目,从而改变状态,修改的部分不说的状态什么关系的关系。这是独立的真正意义 - 它有自己的条目,但不知什么原因,它是很难得到它的DbContext API(EF 4.1)。建议的方法是使用外键关联,而不是独立相关。若要更改您的关联外键,你必须这样做:

This doesn't work because you are using independent association. Relation between TownRecord and TownRecordType is not part of town record's entry so changing state to modified doesn't say anything about state of relation. That is the real meaning of "independent" - it has its own entry but for unknown reason it is hard to get it in DbContext API (EF 4.1). Proposed way is using Foreign key association instead of independent association. To change your association to foreign key you must do this:

public class TownRecord
{
    public int Id { get; set; }
    ...
    [ForeignKey("RecordType")]
    public int RecordTypeId { get; set; }
    public virtual TownRecordType RecordType { get; set; }
    ...
}

您会改变你的代码:

[HttpPost]
public ActionResult Edit(int id, TownRecord tr, FormCollection collection)
{
    tr.RecordTypeId = Int32.Parse(collection["RecordType"]);
    _ctx.TownRecords.Attach(tr);
    _ctx.Entry(tr).State = EntityState.Modified;
    _ctx.SaveChanges();
    return RedirectToAction("List");
}



其实的有人问2小时你问的问题之前,同样的问题的问题。我也试图提供解决方案,它具有独立的协会工作,但我不喜欢它。问题是,独立的关联,你需要连接 TownRecord 加载其实际 TownRecordType ,并用新的替换 TownRecordType

Actually the question with the same problem was asked 2 hours before you asked the question. I also tried to provide solution which works with independent association but I don't like it. The problem is that for independent association you need to have attached TownRecord loaded its actual TownRecordType and replace it with new TownRecordType.

这篇关于实体框架4.1 RC(代码第一次) - 实体没有更新过协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆