3 个表的实体框架映射以及彼此之间的关系
这是我的问题: 我的数据库中有 3 个表
电影(电影列表)
- 身份证
- 原标题 ...
流派(包含所有可能流派的表格)
- 身份证
- 姓名
RelatedGenres(属于特定电影并指向特定流派的流派,因为电影可以有多个流派)
- 身份证
- 电影ID
- 流派ID
关系如下:
Movies.ID ->相关流派.MovieID -> Genres.ID
我有一个具有关联的模型(导航属性)。 我得到的:
class Movie
{
public int ID { get; set; }
public string OriginalTitle { get; set; }
public ObjectCollection<RelatedGenre> RelatedGenres { get; set; }
}
其中
class RelatedGenre
{
public int ID { get; set; }
public int MovieID { get; set; }
public ObjectCollection<Genre> Genres { get; set; }
}
我想要的:
class Movie
{
public int ID { get; set; }
public string OriginalTitle { get; set; }
public ObjectCollection<Genre> Genres { get; set; }
}
正如你所看到的,我想跳过这个RelatedGenres & 数组中的数据。只需获取具体流派数组...
我怎样才能实现这一目标? 预先感谢 =)
Here's my problem:
I have 3 tables in my database
Movies (list of movies)
- ID
- OriginalTitle
...
Genres (table with all possible genres)
- ID
- Name
RelatedGenres (those genres that belog to a specific movie and point a specific genre, since movie can have more than 1 genre)
- ID
- MovieID
- GenreID
The relationships are as folows:
Movies.ID -> RelatedGenres.MovieID -> Genres.ID
I have a model with assosiations (navigation properties).
What I get:
class Movie
{
public int ID { get; set; }
public string OriginalTitle { get; set; }
public ObjectCollection<RelatedGenre> RelatedGenres { get; set; }
}
where
class RelatedGenre
{
public int ID { get; set; }
public int MovieID { get; set; }
public ObjectCollection<Genre> Genres { get; set; }
}
What I want:
class Movie
{
public int ID { get; set; }
public string OriginalTitle { get; set; }
public ObjectCollection<Genre> Genres { get; set; }
}
As you can see, i want to skip data from this array of RelatedGenres & just get array of concrete Genres...
How can I achive this?
Thanks in advance =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要删除
RelatedGenres
表的ID
列。多对多关系的连接表应该只包含参与实体的键。EF 将自动对关系进行建模,如最终代码示例中所示。
You need to remove th
ID
column of theRelatedGenres
table. The join table of many-to-many relationship should only contain the keys of the participating entities.EF will automatically model the relationship as you have shown in the final code sample.