[英]Entity FrameWork Many to Many relationship using Asp.Net
I have 3 tables. 我有3张桌子。 One called Artist, One called Song and one called Album. 一个叫做艺术家,一个叫宋,一个叫专辑。
Artist is linked to song as 1 to many. 艺术家与歌曲的关联为1对多。 Artist is also linked to Album as 1 to many. 艺术家也与Album链接为1对多。 Song is linked to Album as many to many so a bridge table was automatically created. 歌曲与Album链接了许多对,因此自动创建了一个桥接表。 The song table has a navigation property of artist and album and the album has a navigation property of Artist and song. 歌曲表具有艺术家和专辑的导航属性,该专辑具有艺术家和歌曲的导航属性。 I want to add a new song. 我想添加一首新歌。 When adding a song the bridge table doesn't get updated so I want to know how I can reference the album that is associated with the song when adding it. 添加歌曲时,桥接表不会更新,所以我想知道在添加歌曲时如何引用与歌曲相关联的专辑。
public int CreateNewSong(String name,String songTitle)
{
using(var context = new Myentities())
{
Song theNewSong = new Song()
Artist refer = context.Artists.Single(o => o.ArtistName == name);
theNewSong.SongTitle = songTitle;
theNewSong.Artist_ArtistID = refer.ArtistID;
context.Songs.AddObject(theNewSong);
context.SaveChanges();
return theNewSong.SongID;
}
}
First if you have many-to-many relation between Song
and Album
your Song
entity should contain collection of Albums
and the Album
entity should contain collection of Songs
: 首先,如果Song
和Album
之间存在多对多关系,则您的Song
实体应包含Albums
集合,而Album
实体应包含Songs
集合:
public class Song
{
...
public virtual ICollection<Album> Albums { get; set; }
}
public class Album
{
...
public virtual ICollection<Song> Songs { get; set; }
}
Don't forget to map them. 别忘了映射它们。 For example if you use Fluent API add this to your context class: 例如,如果您使用Fluent API将其添加到您的上下文类:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Song>()
.HasMany(s => s.Albums)
.WithMany(a => a.Songs)
.Map(m =>
{
m.MapLeftKey("SongID");
m.MapRightKey("AlbumID");
m.ToTable("SongAlbums");
});
}
So your method will be similar to this one: 所以你的方法将类似于这个:
public int CreateNewSong(String name, String songTitle, String album)
{
using(var context = new Myentities())
{
Album album = context.Albums.Single(o => o.AlbumName == album);
Artist refer = context.Artists.Single(o => o.ArtistName == name);
Song theNewSong = new Song {
SongTitle = songTitle,
Artist = refer
};
theNewSong.Albums.Add(album);
context.Songs.AddObject(theNewSong);
context.SaveChanges();
return theNewSong.SongID;
}
}
I think this happens because you have a 1 to many relationship between song and album so you have to define which album is associated with the song (as you put artist) 我认为这是因为你在歌曲和专辑之间有一对多的关系,所以你必须定义哪一张专辑与歌曲相关(当你放置艺术家时)
public int CreateNewSong(String name,String songTitle,String album)
{
using(var context = new Myentities())
{
Song theNewSong = new Song()
Artist refer = context.Artists.Single(o => o.ArtistName == name);
Album album = context.Albums.Single(o => o.AlbumName == album);
theNewSong.SongTitle = songTitle;
theNewSong.Artist_ArtistID = refer.ArtistID;
theNewSong.Album_AlbumID = album.AlbumID;
context.Songs.AddObject(theNewSong);
context.SaveChanges();
return theNewSong.SongID;
}
}
I think this may work 我认为这可行
Very good answer algreat
, let my highlight 2 points because I've been stucked with an error of EF Many - to - Many relantionship (the corresponding navigation property was sending and exception and not retrieving the information) and it's important: 非常好的答案algreat
,让我突出2点,因为我一直被错误的EF多对多关系(相应的导航属性发送和异常,而不是检索信息),这很重要:
The Mapping in the OnCreatingModel
, if you don't map to the corresponding intermediate table, EF cannot find this object
to perform the join, is the table that's omitted in the edmx
file. OnCreatingModel
中的OnCreatingModel
,如果未映射到相应的中间表,则EF无法找到此object
来执行连接,是edmx
文件中省略的表。
If you are using the tools for generate the entities and the data context, wich is my case, and modify the edmx
file you'll probably have to code this again because all this "generated objects" will refresh to the initial state and your mapping, your data annotations and that stuff will be erased. 如果您正在使用工具来生成实体和数据上下文,那么就是我的情况,并修改edmx
文件,您可能需要再次对此进行编码,因为所有这些“生成的对象”将刷新到初始状态和映射,您的数据注释和那些东西将被删除。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.