Database Reference
In-Depth Information
Listing 2-12.
Creating the Photograph POCO Entity
public class Photograph
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PhotoId { get; set; }
public string Title { get; set; }
public byte[] ThumbnailBits { get; set; }
[ForeignKey("PhotoId")]
public virtual PhotographFullImage PhotographFullImage { get; set; }
}
Create a
PhotographFullImage
POCO entity class using the code in Listing 2-13.
3.
Listing 2-13.
Creating the PhotographFullImage POCO Entity
public class PhotographFullImage
{
[Key]
public int PhotoId { get; set; }
public byte[] HighResolutionBits { get; set; }
[ForeignKey("PhotoId")]
public virtual Photograph Photograph { get; set; }
}
Add an auto-property of type
DbSet<Photograph>
to your DbContext subclass.
4.
Add another auto-property type of
DbSet<PhotographFullImage>
to your DbContext
subclass.
5.
Override the
OnModelCreating()
method of the DbContext class, as shown in Listing 2-14.
6.
Listing 2-14.
Overriding the OnModelCreating Method of DbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Photograph>()
.HasRequired(p => p.PhotographFullImage)
.WithRequiredPrincipal(p => p.Photograph);
modelBuilder.Entity<Photograph>().ToTable("Photograph", "Chapter2");
modelBuilder.Entity<PhotographFullImage>().ToTable("Photograph", "Chapter2");
}