帮助建立许多关系 [英] Help setting up many of many relationships

查看:70
本文介绍了帮助建立许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意这不会崩溃或任何事情,它只是不工作我想要它在这里是一个IMG。

http://s27.postimg.org/vsocn02eb/Untitled.png [ ^ ]

我想要它只是显示电影,其中Jennifer Garners Id在数据库中,所以说她在史莱克,我希望它只用shrek显示她的名字。



我创建了一个包含3个表的SQL数据库。表格设置如下:



电影:

Id,

名称,







演员:

Id,

姓名



ActorsInMovies:

MovieId,

ActorId



我将它们添加到Visual Studio中,它们出现如下:

  namespace  MvcMovieDemo.Data 
{
使用系统;
使用 System.Collections.Generic;

public partial class 演员
{
公开演员()
{
.Movies = new HashSet< movie>();
}

public int Id {获得; set ; }
public string 名称{ get ; set ; }

public virtual ICollection< movie>电影{获取; set ; }
}
}
命名空间 MvcMovieDemo.Data
{
使用系统;
使用 System.Collections.Generic;

public partial class 电影
{
public 电影()
{
.Actors = new HashSet< actor>();
}

public int Id {获得; set ; }
public string 名称{ get ; set ; }
public string 类型{ get ; set ; }
public string 评分{ get ; set ; }

public virtual ICollection< actor>演员{获取; set ; }
}
}





我的控制器设置如下:

< pre lang =CS> var actor = db.Actors。 Single (a = > a.Id == id);
model.PageTitle = actor.Name + ''电影';





整个想法是为演员提供一个详细信息页面,所以你点击了id 19 = emma watson我想展示她的电影。 />


我试过但很难在这里是我的控制器表:

  public  ActionResult Movies( int  id)
{
var model = new MoviesViewModel();
var actor = db.Actors。 Single (a = > a.Id == id);
model.PageTitle = actor.Name + 's'Movies;

model.Actorname = actor.Name; // 设置此项以便我们可以在视图中输出其名称。
var items = db.Movies.OrderBy(i = > i.Name).AsQueryable(); // 这就在这里,因为我将在稍后使用PagedView

< span class =code-keyword> if (!String.IsNullOrEmpty(actor.Name))
{
items = items.Where(i = > actor.Id == id);
}
model.MovieList = items.ToList();

return 查看(模型);
}

解决方案

你在哪里



 items = items.Where(i = >  actor.Id == id); 





这是完全错误的,是的。



你需要做的更像是



items = items.Where(i => i.Actors.Contains(id)



(对不起 - 这不是Linq的有效但是你希望看到我的意思! - 不是在电脑前面的莫和Linq不是我强大的西装!)



所以你想要那部电影的演员集合中包含你指定的Id的演员的电影列表......


Note this doesnt crash or anything, its just not working how I want it to here is an IMG.
http://s27.postimg.org/vsocn02eb/Untitled.png[^]
What I want it to do is only show movies where Jennifer Garners Id is in the DB so say she was in shrek,I want it to show her name with shrek only.

I have created a SQL Database with 3 tables. Tables are set up like:

Movies:
Id,
Name,
etc
etc

Actor:
Id,
Name

ActorsInMovies:
MovieId,
ActorId

I added them into Visual studio and they come up like this:

namespace MvcMovieDemo.Data
{
    using System;
    using System.Collections.Generic;
    
    public partial class Actor
    {
        public Actor()
        {
            this.Movies = new HashSet<movie>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
    
        public virtual ICollection<movie> Movies { get; set; }
    }
}
namespace MvcMovieDemo.Data
{
    using System;
    using System.Collections.Generic;
    
    public partial class Movie
    {
        public Movie()
        {
            this.Actors = new HashSet<actor>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
        public string Genre { get; set; }
        public string Rating { get; set; }
        
        public virtual ICollection<actor> Actors { get; set; }
    }
}



I have my controller setup like:

var actor = db.Actors.Single(a => a.Id == id);
model.PageTitle = actor.Name + "'s' Movies";



The whole idea is to have a details page for actors, so you hit id 19 = emma watson I would like to show her movies.

I tried but failed hard here is my controller table:

public ActionResult Movies(int id)
{
    var model = new MoviesViewModel();
    var actor = db.Actors.Single(a => a.Id == id);
    model.PageTitle = actor.Name + "'s' Movies";

    model.Actorname = actor.Name; // Set this so we can output their name in the view.
    var items = db.Movies.OrderBy(i => i.Name).AsQueryable(); // this is here as I will be using PagedView later on

    if (!String.IsNullOrEmpty(actor.Name))
    {
        items = items.Where(i => actor.Id == id);
    }
    model.MovieList = items.ToList();

    return View(model);
}

解决方案

Where you have

items = items.Where(i => actor.Id == id);



Which is just plain wrong, methinks.

What you need to be doing is more like

items = items.Where(i=>i.Actors.Contains(id)

(sorry - that's not valid Linq but you hopefully see what I mean! - not in front of a PC at the mo and Linq not my strong suit!)

So you want the list of movies where that movie's collection of actors includes the actor with the Id you specified...


这篇关于帮助建立许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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