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.

  • one
    Always, when you want to screw the cache, think about how you will disable it. For it is easy to fasten the cache, but to make a correct disability for it is many times more difficult. - Monk

2 answers 2

You can use Second Level Cache for Entity Framework . It stores data in memory, but instead of InMemoryCache , you can write your own implementation of a cache that stores data on disk.

  • Manual cache invalidation, do I understand correctly? Any magic? =) - Monk
  • @Monk - The moped is not mine ... This cache works in conjunction with the provider, no manual work, AFAIK. - Alexander Petrov

What you need is caching. EF provides caching only for the lifetime of the context. Objects previously requested from the database are stored in memory. When you destroy the context, especially when you restart the program, the cache is reset. And it is right. For your task, you need to use a third-party library or even a specialized product: redis, memcached or similar.