有没有更好的方式来增加使用MVC 4和Entity Framework 5个孩子的记录? [英] Is there a better way to add a child record using MVC 4 and Entity Framework 5?

查看:106
本文介绍了有没有更好的方式来增加使用MVC 4和Entity Framework 5个孩子的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学MVC和处理其无状态特性,结合实体框架。我的问题是,是否有处理下面的场景更优雅的方式?

I am just learning MVC and dealing with its stateless nature in combination with Entity Framework. My question is, is there a more elegant way of handling the scenario below?

我有两个POCO实体:

I have two POCO entities:

public class Contest
{
    public long ID { get; set; }
    ....   .....
    public ICollection<ContestPrize> Prizes { get; set; }
}

public class ContestPrize
{
    public long ID { get; set; }

    public Contest Contest { get; set; }

}

然后我有一个MVC视图显示的竞赛,并与他们相关联的所有奖品。在这个观点我有一个创建奖链接来创建一个通过比赛来的ContestPrize控制器像这样的ID的新奖项:

I then have a MVC View that displays the Contest and all the Prizes associated with them. On that view I have a "Create Prize" link to create a new prize which passes the ID of the contest to the ContestPrize Controller like so:

@Html.ActionLink("Create Prize", "Create", "ContestPrizes", new { ContestId = Model.ID }, null)

该ContestId是坚持为在提交新的奖金形式的隐藏字段,这样我可以检索上创建。该奖项控制器上的创建方法如下:

The ContestId is persisted as a hidden field in the form that submits a new prize so that I can retrieve it on create. The create method on the prize controller is below:

    [HttpPost]
    public ActionResult Create(int ParentID, ContestPrize ContestPrize)
    {


        if (ModelState.IsValid)
        {

            db.ContestPrizes.Add(ContestPrize);
            db.SaveChanges();
            return RedirectToAction("Index", "Contests", ParentID);
        }

        return View(ContestPrize);
    }

这是我碰到一些丑陋,我不喜欢。这里的问题:

This is where I run into some ugliness that I don't like. Here are the problems:


  1. ModelState.IsValid 测试失败。这是因为,对于母体的导航属性是基于模型空。它没有被填充。我可以跳过对模型的验证和向右走到数据库,但有什么办法来填充这个而不做如下#2?

  2. 父属性没有在这一点上填充,但我有从隐藏的表单字段来在PARENTID。既然一切是无状态的,数据库对象没有父对象(这是罚款)。所以,我可以执行读来得到使用的parentID父对象另一个数据库,然后执行 Parent.Prizes.Add(ContestPrize) ContestPrize.Contest =父。但我真的,因为我在做一个额外的数据库击中读取父不喜欢这样,当我拥有所有的信息,我需要创建子记录。

  1. The ModelState.IsValid test fails. This is because the navigation property for the parent is null on the model. It hasn't been populated. I can skip the validation on the model and go right to the database but is there a way to populate this without doing #2 below?
  2. The parent property isn't populated at this point, but I do have the ParentID that comes in from the hidden form field. Since everything is stateless, the db object doesn't have the parent object (which is fine). So, I could perform another database read to get the parent object using the parentID and then do Parent.Prizes.Add(ContestPrize) or ContestPrize.Contest = Parent. But I really don't like this because I am making an extra database hit to read the parent, when I have all the information I need to create the child record.

那么,有没有办法与实体框架只设置父的ID和挽救一个孩子,而无需检索父对象?还是......最好是有使用MVC以一种方式pre-填充与父对象创建,因为我确实有家长标题进入创建视图之前的新模式?这将是多么糟糕的是通过周围的ViewData父对象也许是这样,这是可用的?

So, is there a way with Entity Framework to just set the ID of the parent and save a child without having to retrieve the parent object? Or...ideally is there a way using MVC to pre-populate the new model on create with a parent object since I do have the parent before heading into the create views?? How bad would it be to pass around the parent object in ViewData maybe so that it was available?

我可以通过重装父,使这项工作。但我想,以避免可能的话。

I can make this work by reloading the parent. But I want to avoid it if possible.

思考?

更新:添加 ContestID 来的ContestPrize POCO,然后用装修导航属性[ForeignKey的(ContestID )] 作为@NSGaga建议作品完美,消除了一些绕过的问题杂乱PARENTID的。新波苏斯看起来是这样的:

UPDATE: Adding the ContestID to the ContestPrize POCO and then decorating the navigation property with [ForeignKey("ContestID")] as @NSGaga suggested works perfectly and eliminates some of the messy ParentID passing around problems. The new POCOs looks like this:

public class Contest
{
    public long ID { get; set; }
    ....   .....
    public ICollection<ContestPrize> Prizes { get; set; }
}

public class ContestPrize
{
    public long ID { get; set; }

    public long ContestID { get; set; }

    [ForeignKey("ContestID")]
    public Contest Contest { get; set; }

}

我可以消除PARENTID传递和意见作为一个隐藏字段,因为数据在MVC绑定需要照顾ContestID对新的实体(我还是要ID传递到初始视图设置,但仅此而已它只是从那里开往上了)。控制器上的新创建的动作看起来是这样的:

I can eliminate the ParentID being passed and set in the views as a hidden field because data binding in MVC takes care of the ContestID on the new entity (I still have to pass the id into the initial view, but that is all. It is just bound from there on out). The new Create action on the Controller looks like this:

[HttpPost]
public ActionResult Create(ContestPrize ContestPrize)
{
    if (ModelState.IsValid)
    {
        db.ContestPrizes.Add(ContestPrize);
        db.SaveChanges();
        return RedirectToAction("Index", "Contests", ContestPrize.ContestID);
    }

    return View(ContestPrize);
}

清洁并没有乱简单。我喜欢它。

Clean and simple with no mess. I like it.

推荐答案

如果你使用'code第一个(万物建议),那么你可以做这样的事情...

If you're using 'code first' (all things suggest), then you can do something like...

public class ContestPrize
{
    public long ID { get; set; }

    public long ContestID { get; set; }
    public Contest Contest { get; set; }
}

这应该创建(按照约定)和映射ContestID到FK。在一些复杂的情况下,您可能需要指定的流畅code的关系,但这应该足够了。

That should create (by convention) and map the ContestID to the FK. In some complicated cases you may need to specify that relationship in the fluent code, but this should suffice normally.

然后,你可以通过 ContestID 只是引用父 - 填写在 -
  做添加

Then you can just reference the parent by ContestID - fill that in - and do Add.

这是粗糙的笔触,让我知道如果你需要更多的信息。

That's in rough strokes, let me know if you need more info.

这篇关于有没有更好的方式来增加使用MVC 4和Entity Framework 5个孩子的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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