The program recursively processes directories and through COM reads the specific properties of certain files. New properties can be defined by the user, and be available only for a specific file. When reading properties, I need to check if there is already such a property in the database and to add it in its absence, but how to avoid a query to the database for each comparison?
Also I can not figure out how after saving the record in Files and in Property add an entry in FileProperties?
Thank you in advance for your time and attention.
foreach(var path in paths){ Files file; using(var context = new DbContext()){ file = context.FirstOrDefault(f => ... ); } if(file == null){ file = new Files(){ ... }; using(var context = new DbContext()){ context.Files.Add(file); context.SaveChanges(); } var specProps = GetSpecProps(path); using(var context = new DbContext()){ foreach(var specProp in specProps){ Property prop; // отправляет запрос на сервер, а как читать из "кеша"? prop = context.Property.FirstOrDefault( p => ...); if(prop == null){ prop = new Property(){ ... }; context.Property.Add(prop); } // как здесь добавить запись в FileProperties? // System.Data.Entity.Infrastructure.DbUpdateException: // Не удалось обновить набор EntitySet "FileProperties", // поскольку в нем имеется запрос DefiningQuery и // отсутствует элемент <InsertFunction> в элементе // <ModificationFunctionMapping> для поддержки текущей операции. // context.FileProperties.Add(new FileProperties() { // File = file, // Property = prop, // property_value = specProp.Value.ToString() // }; } context.SaveChanges(); } } } 