通过控制器方法(实体)更新数据库关系

时间:2019-09-17 16:32:25

标签: c# .net entity

尝试更新数据库中的多对多关系,如下所示:

DeckCards
    (key sign) Deck_id
    (key sign) Card_id

我正在尝试通过控制器操作执行此操作:

    public ViewResult Add(int id)
    {
        var cardInDb = _context.Cards.Single(c => c.Id == id);

        var deckInDb = _context.Decks.Single(d => d.id == id);

        deckInDb.Card.Add(cardInDb);
        cardInDb.Deck.Add(deckInDb);

        return View("Index");
    }

我的观点:

@model YGOBuilder.Models.ViewModels.DeckCardViewModel

<div>
    <h4>DeckCardViewModel</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Cards.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cards.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Cards.Atk)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cards.Atk)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Cards.Def)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cards.Def)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Cards.Desc)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cards.Desc)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Cards.Level)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cards.Level)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Cards.Type)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cards.Type)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Cards.Attribute)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cards.Attribute)
        </dd>

        @foreach (var item in Model.Cards.Card_Images)
        {

            <dd>
                <img src=@item.image_url height="300" width="200" style="margin: 2px">
            </dd>

        }

    </dl>


    @foreach (var d in Model.Decks)
    {
    <ul class="nav navbar-nav navbar-right">
        @Html.ActionLink($"Add to: {d.Name} ", "Add", new { id = d.id })
    </ul>
    }


</div>

我要寻找的最终结果是数据库表正在像这样更新DeckCards表:

Deck_Id     Card_Id
    3            1

ID为1的卡映射到Deck3。

我的模特:

public class Card
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? Atk { get; set; }
    public int? Def { get; set; }
    public string Desc {get; set;}
    public int? Level { get; set; }
    public string Type { get; set; }
    public string Attribute { get; set; }
    [DisplayName("Image")]
    public virtual List<Image> Card_Images { get; set; }

    public virtual List<Deck> Deck { get; set; }


}

public class Deck
{
    public int id { get; set; }

    public string Name { get; set; }

    public string Notes { get; set; }
    [DisplayName("Card")]
    public virtual List<Card> Card { get; set; }
}

当我运行上面的代码时,var cardInDb = _context.Cards.Single(c => c.Id == id);似乎与序列中断不包含任何元素错误。

1 个答案:

答案 0 :(得分:0)

您正在尝试获取具有卡座ID的卡。您需要从视图中传递DeckId和CardId到Add方法,并使用它们来获得Card和Deck。

已更新

您需要执行以下操作:

@Html.ActionLink($"Add to: {d.Name} ", "Add", new { cardId = Model.Cards.Id, deckId = d.id })

...

public ViewResult Add(int cardId, int deckId)
{
    var cardInDb = _context.Cards.Single(c => c.Id == cardId);

    var deckInDb = _context.Decks.Single(d => d.id == deckId);

    deckInDb.Card.Add(cardInDb);
    cardInDb.Deck.Add(deckInDb);

    return View("Index");
}

添加项目后,也不要忘记保存更改。

_context.SaveChanges();