Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Microsoft Reference:</strong></p> <p><a href="http://msdn.microsoft.com/en-gb/library/cc853327.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-gb/library/cc853327.aspx</a></p> <p>They say;</p> <blockquote> <p>Limit the scope of the ObjectContext</p> <p>In most cases, you should create an ObjectContext instance within a using statement (Using…End Using in Visual Basic). </p> <p>This can increase performance by ensuring that the resources associated with the object context are disposed automatically when the code exits the statement block. </p> <p>However, when controls are bound to objects managed by the object context, the ObjectContext instance should be maintained as long as the binding is needed and disposed of manually. </p> <p>For more information, see Managing Resources in Object Services (Entity Framework). <a href="http://msdn.microsoft.com/en-gb/library/bb896325.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-gb/library/bb896325.aspx</a></p> </blockquote> <p>Which says;</p> <blockquote> <p>In a long-running object context, you must ensure that the context is disposed when it is no longer required.</p> </blockquote> <hr> <p><strong>StackOverflow Reference:</strong></p> <p>This StackOverflow question also has some useful answers... </p> <p><a href="https://stackoverflow.com/questions/3957485/entity-framework-best-practices-in-business-logic">Entity Framework Best Practices In Business Logic?</a></p> <p>Where a few have suggested that you promote your context to a higher level and reference it from here, thus keeping only one single Context.</p> <hr> <p><strong>My ten pence worth:</strong></p> <p>Wrapping the Context in a Using Statement, allows the Garbage Collector to clean up the resources, and prevents memory leaks.</p> <p>Obviously in simple apps, this isn't much of a problem, however, if you have multiple screens, all using alot of data, you could end up in trouble, unless you are certain to Dispose your Context correctly.</p> <p>Hence I have employed a similar method to the one you have mentioned, where I've added an <code>AddOrUpdate</code> Method to each of my Repositories, where I pass in my New or Modified Entity, and Update or Add it depending upon whether it exists.</p> <hr> <p><strong>Updating Entity Properties:</strong></p> <p>Regarding updating properties however, I've used a simple function which uses reflection to copy all the properties from one Entity to Another;</p> <pre><code>Public Shared Function CopyProperties(Of sourceType As {Class, New}, targetType As {Class, New})(ByVal source As sourceType, ByVal target As targetType) As targetType Dim sourceProperties() As PropertyInfo = source.GetType().GetProperties() Dim targetProperties() As PropertyInfo = GetType(targetType).GetProperties() For Each sourceProp As PropertyInfo In sourceProperties For Each targetProp As PropertyInfo In targetProperties If sourceProp.Name &lt;&gt; targetProp.Name Then Continue For ' Only try to set property when able to read the source and write the target ' ' *** Note: We are checking for Entity Types by Checking for the PropertyType to Start with either a Collection or a Member of the Context Namespace! ' If sourceProp.CanRead And _ targetProp.CanWrite Then ' We want to leave System types alone If sourceProp.PropertyType.FullName.StartsWith("System.Collections") Or (sourceProp.PropertyType.IsClass And _ sourceProp.PropertyType.FullName.StartsWith("System.Collections")) Or sourceProp.PropertyType.FullName.StartsWith("MyContextNameSpace.") Then ' ' Do Not Store ' Else Try targetProp.SetValue(target, sourceProp.GetValue(source, Nothing), Nothing) Catch ex As Exception End Try End If End If Exit For Next Next Return target End Function </code></pre> <p>Where I do something like;</p> <pre><code>dbColour = Classes.clsHelpers.CopyProperties(Of Colour, Colour)(RecordToSave, dbColour) </code></pre> <p>This reduces the amount of code I need to write for each Repository of course!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload