实体框架4.1 RC(代码第一次)

编程入门 行业动态 更新时间:2024-10-26 13:28:46
本文介绍了实体框架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(代码第一次)

本文发布于:2023-11-16 17:10:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606226.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   代码   RC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!