/// <summary> /// Searches the context for a trait that matches the given name, creating one if none are found /// </summary> internal static Trait GetTraitByName(this IRemsDbContext context, string name) { var trait = context.Traits.ToArray().FirstOrDefault(t => t.NameMatches(name)); if (trait is null) { trait = new Trait() { Name = name }; context.Add(trait); } context.SaveChanges(); return(trait); }
/// <summary> /// Create an entity of the given type, and attach it to the database after attempting to assign /// one of its properties the given value /// </summary> /// <param name="context">The context to attach the entity to</param> /// <param name="type">The CLR type of the <see cref="IEntity"/></param> /// <param name="value">The value to attempt to assign</param> /// <returns></returns> public static IEntity CreateEntity(this IRemsDbContext context, Type type, object value) { IEntity entity = Activator.CreateInstance(type) as IEntity; var name = type.GetProperty("Name"); if (name is null) { type.GetProperty("SoilType")?.SetValue(entity, value); } else { name?.SetValue(entity, value); } context.Attach(entity); context.SaveChanges(); return(entity); }
/// <summary> /// Adds a trait to the database /// </summary> /// <param name="name">Trait name</param> /// <param name="type">Trait type</param> /// <returns></returns> internal static Trait AddTrait(this IRemsDbContext context, string name, string type) { var unit = context.Units.FirstOrDefault(u => u.Name == "-"); if (unit is null) { unit = new Domain.Entities.Unit() { Name = "-" } } ; var trait = new Trait() { Name = name, Type = type, Unit = unit }; context.Add(trait); context.SaveChanges(); return(trait); }