Developing a desktop application. To work with the database I use the Entity Framework (Code First approach).
In the database there is a table People
CREATE TABLE [dbo].[People] ( [id] [int] IDENTITY(1,1) NOT NULL, [FirstName] [nvarchar](50) NOT NULL, [PhotoOriginal] [varbinary](max) NULL ) In EF, two entities are associated with it: Person ( id , Name , PhotoOriginal )
[Table("Person")] public partial class Person:INotifyPropertyChanged { public Person() { Result_question = new HashSet<Result_question>(); } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int id { get; set; } [Required] [StringLength(50)] public string FirstName{get;set;} PhotoOriginal _PhotoOriginal; virtual public PhotoOriginal PhotoOriginal { get { return this._PhotoOriginal; } set { if (this._PhotoOriginal != value) { this._PhotoOriginal = value; this.NotifyPropertyChanged("PhotoOriginal"); } } } and PhotoOriginal ( id , Image ).
[Table("Person")] public partial class PhotoOriginal:INotifyPropertyChanged { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int id { get; set; } byte[] _Image; [Column("PhotoOriginal")] public byte[] Image { get { return this._Image; } set { if (this._Image != value) { this._Image = value; this.NotifyPropertyChanged("Image"); } } } virtual public Person person { get; set; } } Code from DBModel
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<PhotoOriginal>() .HasKey(e => e.id); modelBuilder.Entity<Person>() .HasKey(e => e.id) .HasRequired(e => e.PhotoOriginal) .WithRequiredPrincipal(e => e.person); modelBuilder.Entity<Person>().ToTable("People"); modelBuilder.Entity<PhotoOriginal>().ToTable("People"); } I want, that at the first reference to the person.PhotoOriginal property person.PhotoOriginal image is loaded from the database and saved to the local disk. On subsequent calls (after restarting the program too), EF did not climb into the database, but pulled it up from the disk. How can you organize such work? How to modify entities? Maybe which handler to subscribe to and catch the EF appeal to the database? Prioritize not writing a wrapper for entities.