Forum Posts

Alex_SCS
Jun 17, 2022
In Entity Framework
What is the best way to dispose the DbContexts of my active business objects and reset the environment while maintaining all parent-child relationships between those business objects? My scenario is as follows: App is launched and initializes all required business objects including registering child business objects. User makes changes and saves them. App refreshes all data queried to reflect any and all pre and post-save procedures to the user How do I make sure the DbContexts of all my business objects are disposed and reinitialized to ensure the next query is pulled from the database and not EF's cache?
0
0
1
Alex_SCS
Sep 18, 2020
In Entity Framework
Greetings from Silver Creek Software! We are currently implementing Mere Mortals in our project, and I thought we could kick things off in this new forum by sharing a customization/enhancement we've made to the base Business Object (sometimes referred to as "ABusinessObject") class definition to generate a DataTable from a list of entities: /// <summary> /// Converts a collection of <see cref="EntityType"/> objects to a <see cref="DataTable"/> for easier user-consumption /// </summary> /// <param name="pEntityList">(Optional) Target collection, uses <see cref="EntityList"/> if unspecified</param> /// <returns>New <see cref="DataTable"/> with data from entities</returns> public DataTable ToDataTable(IEnumerable<EntityType> pEntityList = null) { DataTable dataTable = new DataTable(); IEnumerable<EntityType> entityList = pEntityList != null ? pEntityList : EntityList; // Create the columns by reflecting the entity type's properties // (TO-DO: Take advantage of the "DisplayName" data annotation (or Culture-sensitive resource file) as an alternative to using the property name) typeof(EntityType).GetProperties().ToList() .ForEach(x => dataTable.Columns.Add(x.Name, Nullable.GetUnderlyingType(x.PropertyType) ?? x.PropertyType)); // Generate rows foreach (EntityType entity in entityList) { DataRow newRow = dataTable.NewRow(); typeof(EntityType).GetProperties().ToList() .ForEach(x => newRow[x.Name] = x.GetValue(entity) ?? DBNull.Value); dataTable.Rows.Add(newRow); } return dataTable; }
0
1
67

Alex_SCS

More actions