外键约束,EF与childobjects集合 [英] Foreign key constraint, EF with collection of childobjects

查看:244
本文介绍了外键约束,EF与childobjects集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新模式,但得到的错误操作失败:关系不能被改变,因为一个或多个外键的属性是不可为空当变化有关系的。 ,相关的外键属性设置为空值,如果外键不支持空值,一个新的关系必须定义,外键属性必须指定一个非空值,或者无关的对象必须删除。

I'm trying to update a model, but get the error "The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted."

这是我从<一个明白href=\"http://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign-key-pro\">The关系不能被改变,因为一个或多个外键的属性是不可为空问题可能与实体框架是如何处理我的虚拟的ICollection

From what I understand from The relationship could not be changed because one or more of the foreign-key properties is non-nullable the problem might be with how Entity Framework handles my virtual ICollection

不过,我真的不知道如何使用脚手架存储库模式时,实施解决方案。我一定要编辑保存() - 方法ParentObjectRepository级

However I'm not really sure how to implement the solution when using scaffolded repository pattern. Do I have to edit the Save()-method ParentObjectRepository-class?

其实我真的觉得必须有一些方法,使EF明白这一点。我看不到EF-团队如何想:也许没有人使用对象的集合与外键约束,让不支持。

Actually I really think that there must be some way to make EF understand this. I can't see how the EF-team was thinking "Probably noone is using a collection of objects with a foreign key constraint, lets not support that".

更新
增加了code

Update Added code

[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
    var eventRepository = new MagnetEventRepository();
    var original = eventRepository.Find(id);
    UpdateModel(original);
    eventRepository.Save();
    return RedirectToAction("Details", "Home", new { slug = original.Slug });
}

public void Save()
{
    context.SaveChanges();
}

更多code:

public class MagnetEvent
{
    public virtual int Id { get; set; }

    [Required]
    public virtual string Name { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm}")]
    [DataType(DataType.DateTime)]
    public virtual DateTime? StartDate { get; set; }

    public virtual string Description { get; set; }

    [StringLength(100)]
    public virtual string Slug { get; set; }

    public virtual int MaximumCapacity { get; set; }

    [DataType(DataType.Currency)]
    public virtual int TicketPrice { get; set; }

    public virtual int LocationId { get; set; }
    public virtual Location Location { get; set; }

    public virtual Collection<Ticket> Tickets { get; set; }

    public virtual Collection<AttendeeInformationField> CaptureAttendeeInformationFields { get; set; }

    public virtual int CustomerId { get; set; }

    [Required]
    public virtual CUSTOMER Customer { get; set; }
}

的保存() - 方法是从MagnetEventRepository,它是从上述的类脚手架

The Save()-method is from MagnetEventRepository, which is scaffolded from the above class.

另一个更新
我在AttendeeInformationField改变MagnetEventId可空INT成功删除了错误。当检查数据库我可以清楚地看到什么是错的。

Another update I successfully removed the error by changing MagnetEventId in AttendeeInformationField to nullable int. When examining the database I can see exactly what's wrong.

让我们说我有一个值为电子邮件的单一AttendeeInformationField。当编辑我MagnetEvent的AttendeeInformationField更新MagnetEventId为空,然后添加一个新的职位以正确的MagnetEventId和价值。

Let's say I have a single AttendeeInformationField with the value "E-mail". When I edit my MagnetEvent, the AttendeeInformationField updates the MagnetEventId to null and then adds a new post with the correct MagnetEventId and Value.

我倒是很preFER如果AttendeeInformationField的职位,而不是更新。

I'd very much prefer if the posts in AttendeeInformationField were updated instead.

推荐答案

我找到了解决我的问题。这似乎是在ASP.NET MVC中的错误(?),当它涉及到的UpdateModel和含有ICollection的一个典范。

I found a solution to my problem. It seems to be a bug (?) in ASP.NET MVC when it comes to UpdateModel and a model containing an ICollection.

解决方案是覆盖默认行为,因为在这个博客帖子描述:的 HTTP://www.$c$ctuning.net/blog/post/Binding-Model-Graphs-with-ASPNETMVC.aspx

The solution is to override the default behaviour, as described in this blog post: http://www.codetuning.net/blog/post/Binding-Model-Graphs-with-ASPNETMVC.aspx

更新
我发现了一个解决方案!更新集合中的现有项目时,以上才做。为了解决这个问题,我必须手动检查并添加新AttendeeInformationFields。像这样的:

Update I found a solution! The above only worked when updating existing items in the collection. To solve this, I have to manually check and add new AttendeeInformationFields. Like this:

[HttpPost]
public ActionResult Edit(int id, MagnetEvent magnetEvent)
{
    var eventRepository = new MagnetEventRepository();
    var original = eventRepository.Find(id);
    UpdateModel(original);
    foreach (var attendeeInformationField in magnetEvent.CaptureAttendeeInformationFields)
    {
        var attendeeInformationFieldId = attendeeInformationField.Id;
        if (original.CaptureAttendeeInformationFields.AsQueryable().Where(ai => ai.Id == attendeeInformationFieldId).Count() == 0)
        {
            original.CaptureAttendeeInformationFields.Add(attendeeInformationField);
        }
    }
    eventRepository.Save();
}

连同修改后的DefaultModelBinder,这其实与两个编辑和添加工作。现在我还没有试过删除。

Together with the modified DefaultModelBinder, this actually works with both editing and adding. For now I haven't tried deleting.

不过,我希望有一个更简单的方法来做到这一点。好像很多编码做一个非常基本的任务。

Still, I hope there is a simpler way to do this. Seems like a lot of coding to do a very basic task.

这篇关于外键约束,EF与childobjects集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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