I am writing a photo manager application where the Users can collect photos into albums. So I have a many-to-many relationship between Photos and Albums. I can add a photo to an album, and it saves to the database.
I expect that next time I open the app album will contain the photo I added to it, but it doesn't. album.Photos contains 0 Photos. Database contains the row with desired data, so the problem must be in the code.
It worked somehow, but then I started to edit some application functions and it broke. I have tried to change everything back, but it didn't work. I also looked at basic examples of many-to-many relationships in Entity Framework, but I saw no mistakes.
I use SQL Server, if it matters
My Album class:
public class Album
{
public Album()
{
Photos = new List<PhotoInfo>();
}
public Album(string _name)
{
Name = _name;
Photos = new List<PhotoInfo>();
}
public void Rename(string new_name)
{
Name = new_name;
}
public override string ToString()
{
return this.Name;
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<PhotoInfo> Photos { get; set; }
}
My Photo class:
public class PhotoInfo
{
public PhotoInfo()
{
Albums = new List<Album>();
}
public PhotoInfo(string path)
{
Source = path;
FileName = Path.GetFileName(path);
}
public int Id { get; set; }
public string Source { get; set; }
public string FileName { get; set; }
public ICollection<Album> Albums { get; set; }
}
Data context:
class PhotoContext : DbContext
{
public PhotoContext() : base("DefaultConnection") { }
public DbSet<PhotoInfo> Photos { get; set; }
public DbSet<Album> Albums { get; set; }
}
Data loading example:
db = new PhotoContext();
db.Photos.Load();
db.Albums.Load();
Album album = db.Albums.Find(1);