繁体   English   中英

实体框架 6:实体和关系

[英]Entity Framework 6: entities and relationships

我是一个独奏和非常初学者的学习者。 我正在尝试使用 EF6 使用数据库创建一个简单的代码优先应用程序。 我不明白如何通过前端将实体的数据插入另一个实体。 我有两个实体:

public class Movie
{
    [Key]
    public int Id { get; set; }

    public string Title{ get; set; }  

    public int ActorId { get; set; }

    public ICollection<Actor> Actors { get; set; }    
}
public class Actor
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    [ForeignKey("MovieId")]
    public ICollection<Movie> Movies { get; set; }
}

controller。

public ActionResult AddMovie()
{
    var actorsList = (from Name in ctx.Attors select Name).ToList();
    ViewBag.Actors = new SelectList(actorsList, "Name", "Name");
    return View(new Film());
}

[HttpPost]
public ActionResult PerformAddMovie(Movie m)
{
    try
    {
        ctx.Movies.Add(m);
        ctx.SaveChanges();
        return RedirectToAction("Index", "Home");
    }
    catch(Exception ex)
    {
        ModelState.AddModelError("", ex.Message);
    }
    return RedirectToAction("Index", "Home");
}
@model Cinema.Models.Movie
    
@{
    ViewBag.Title = "AddMovie";
}

<h2>AddFilm</h2>

@{ 
    var list = ViewBag.Actors as SelectList;
}


@using (Html.BeginForm("PerformAddMovie", "Movie", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    
<div class="form-horizontal">
    <h4>Film</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ActorId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ActorId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ActorId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Actors, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.Actors, list, "---Select---", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Actors, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
    }
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

通过前端 web 页面将一些电影添加到数据库后,在 addmovie web 页面中,我可以 select 其中一个由下拉列表创建,并且当我保存电影时,其中一个总是在里面创建的电影空的。 我究竟做错了什么?

Model 是错误的

public class Movie
{
    [Key]
    public int Id { get; set; }

    public string Title{ get; set; }  

    public int ActorId { get; set; }

    public virtual Actor Actor { get; set; }    // It should be one to one relationship
}

public class Actor
{
[Key]
public int Id { get; set; }

public string Name { get; set; }

//[ForeignKey("MovieId")]    This is unneccessary
public ICollection<Movie> Movies { get; set; }
}

然后你可以在 select 列表中显示演员姓名时,您可以 select 将演员 ID 作为键

ViewBag.Actors = new SelectList((from s in db.Actor 
                                 select new {Id = s.Id, Name = s.Name }), 
                                  "Id", "Name");

在您的 html 下删除它,因为 Id 已附加到下拉列表中

 <div class="form-group">
        @Html.LabelFor(model => model.ActorId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ActorId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ActorId, "", new { @class = "text-danger" })
        </div>
    </div>

将下拉列表更改为此

<div class="form-group">
        @Html.LabelFor(model => model.ActorId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.ActorId, (Selectlist)ViewBag.Actor, "---Select---", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.ActorId, "", new { @class = "text-danger" })
        </div>
    </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM