Home > database >  C# Entity Framework Core One Way Relationship
C# Entity Framework Core One Way Relationship

Time:10-20

I have the following two classes:

    public class Album
    {
        public int Id { get; set; }
        
        public int? CoverPhotoId { get; set; }

        public Photo Cover { get; set; }
        ...
    }

And

    public class Photo
    {
        public int Id { get; set; }
        ...
    }

An album can (or not) have a cover (photo), the CoverPhotoId column stores the id of the photo.

I'm trying to fill the Cover variable, which should be really simple (atleast with SQL).

However with Entity Framework (using modelbuilder (OnModelCreating(ModelBuilder builder)) I can't seem to get it working.

CodePudding user response:

You should change your models like:

    public class Album
    {
        public int Id { get; set; }
        public Photo Cover { get; set; }
        ...
    }

    public class Photo
    {
        public int AlbumId { get; set; }
        public Album Album { get; set; }
        ...
    }

one Album : one Photo this will be done by making a relation between their keys

and in OnModelCreating:

        modelBuilder.Entity<Album>()
                .HasKey(p => p.Id);

        modelBuilder.Entity<Album>()
                .HasOne<Photo>(a => a.Cover)
                .WithOne(p => p.Album)
                .HasForeignKey<Photo>(p => p.AlbumId);

        modelBuilder.Entity<Photo>()
                .HasKey(p => p.AlbumId);

resource

CodePudding user response:

Try this

public class Album
    {
        public int Id { get; set; }
        
        public int? CoverPhotoId { get; set; }

        public virtual Photo CoverPhoto { get; set; }
        ...
    }

I changed the name of the Cover to CoverPhoto because (I could be wrong on this, but I think) when you use a virtual object, Entity Framework will actually create an int variable in the database that is the variable name with Id appended to it, so if your photo is called CoverPhoto then the variable in the database will be called CoverPhotoId, which you already have so it wouldn't need to create another called CoverId. Now Entity Framework should know that you have a reference to a different table, as long as you have a DbSet for both Albums and CoverPhotos in your DBContext and you can tell it to include that when the Album is queried. You can do this using Lazy or Eager loading or you can tell it to include it in your query directly:

var albums = DBContext.Albums.Include(a => a.CoverPhoto).ToList();

I think that in this case if your Album doesn't have a CoverPhoto then the result would just be an Album reference with a null CoverPhoto but I don't think that it would crash, you would just need to make sure that you check if its null before accessing it.

You can even use the DBModelBuilder to define the CoverPhoto as required or optional, which it sounds like in your case you would want optional. But this link explains that in better detail.

  • Related