I would like to simply create a relationship between two entities that already exists..
In my case I have an Album entity which might have multiple Genres(which also is a bunch of entities)
My Album model looks like this:
public class AlbumModel : IModel
{
public List<GenreModel> Genres { get; set; }
public ArtistModel Artist { get; set; }
public Guid Id { get; set; }
public string Title { get; set; }
public float Price { get; set; }
public string ArtUrl { get; set; }
}
I then want to create a relation ship between a Album model-object and a bunch of GenreModel-objects/entities..
In the "normal" entity framework I would just assign a new EntityKey.. but Im not quite sure of how to do this using code first.. I also noticed that some people has an extra property called xxxxId for the entity they want to create a reference between.. and then simply assign a value to the xxxxId property which some how magically creates the reference between the entities.. and I guess this works fine for a one to one or a one to many relationship.. but I guess that doesnt work for a many to many relationship..or?
Anyway.. this is my GenresModel:
public class GenreModel : IModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<AlbumModel> Albums { get; set; }
}
And this is what I tried before.. but this wont create the relationship..it will only simply create an additional entity/row in my database..
var artistRepository = new ArtistRepository();
var genresRepository = new GenreRepository();
#region Lordi
context.Albums.Add
(
new AlbumModel
{
Artist = artistRepository.GetByName("Lordi"),
ArtUrl = "http://upload.wikimedia.org/wikipedia/en/8/8f/The_Monster_Show.jpg",
Genres = new List<GenreModel> { genresRepository.GetByName("Rock"), genresRepository.GetByName("Hard rock") },
Id = Guid.NewGuid(),
Price = 3,
Title = "The Monster Show",
}
);
context.Albums.Add
(
new AlbumModel
{
Artist = artistRepository.GetByName("Lordi"),
ArtUrl = "http://www.nuclearblast.de/static/articles/157/157892.jpg/1000x1000.jpg",
Genres = new List<GenreModel> { genresRepository.GetByName("Rock"), genresRepository.GetByName("Hard rock") },
Id = Guid.NewGuid(),
Price = 10,
Title = "Zombilation - The Greatest Cuts",
}
);
#endregion
...and for the record... no I dont listen to Lordi :).. just dummy data.. and for some wierd spooky reason Lordi was the first band that came in mind..
Thanks in advance!