Posts

Showing posts from September, 2020

Entity Framework Core Find method code samples

At one time or another a record needs to be located, usually developers will use FirstOrDefault or SingleOrDefault (along with First and Single) to find a single records. When working with a primary key using the Find  and  FindAsync  method will find entities in the added state that are not yet persisted while the Where method will not and need to query the database. Also, in many cases Find will be a good deal faster than Where. What developers need to do is learn what methods are available and work with the appropriate method for a specific task e.g. a model has City name, using Find will not work as it's used on primary keys while FirstOrDefault to find the first city interested in or Where to find all cities interested in. Here the Customers table has a single primary key, FindAsync will if a entity is located with the key passed in keys will record an entity while navigation properties will not be included. See in repository . public   static   async   Task < Customers &g

Pet peeves when coders ask questions

Something seen repeatedly is code that does not make sense while this is one variation.  For instance the following line of code (where BS is a BindingSource) where the developer knows the type. What would be appropriate rather than TryCast is CType or DirectCast with a meaningful variable name. The person wants to update the current record and is not updating. Dim  r =  TryCast ( BS .Current,  DataRowView ) When uses the following to set a value r ( "Area" ) =  calculatedresult They had the following for updating on property changed dataTable.Columns.Add( New   DataColumn ()  With  {                              .ColumnName =  "Area" ,                              .DataType =  GetType ( Decimal )                          }) Why isn't the value being updated? Because they went against the DataRowView rather than the DataRow -> column. I recommended using the following. Public   Class   CodeSampleForm      Private   ReadOnly   _bindingSource   As  

Entity Framework/Entity Framework Core connection strings for Windows Forms

Image
When creating a DbContext for Entity Framework 6 or Entity Framework Core by default the constructor for Entity Framework 6 has the name for the connection string and for Entity Framework Core is embedded in OnConfigurating. For personal usage and this is fine while in professional applications there are two considerations, first can someone use the connection string maliciously to gain access to data and secondly, developers should have separate environments for testing. In the following repository there are code samples for encrypting/decrypting connection strings and code samples for working with development, staging and production environments. Each project has a readme file which explains how to implement in a project. Although the majority of code samples are C# using Entity Framework Core there is an example for C# Entity Framework 6 and one example for Entity Framework Core with VB.NET along with unit test. Running code samples Using SSMS (SQL-Server Management Studio), create

Sometimes VB.NET coders make me wonder (or crazy)

 The following was taken from a die hard VB.NET coder. I believe C# promotes bloated, slow, inefficient, and buggy code, largely in part because people care more about the cosmetics of their code than they do its actual functionality, efficiency, and reusability. Wow, that is one broad stereotype while I've seen more VB.NET write really poor code more than I've seen C# developer and I'm a proponent for VB.NET coders but not for those who think the way as the above person. No matter what language a person works with there are both bad and good coders while from what I've seen between VB.NET and C# from non professionals VB.NET coders are those who tend to write unclean and loose code while for the most part C# coders that are not working professionally really can write sloppy code as a VB.NET coders does simply because a VB.NET coder can turn Option Strict Off or actually never turn it on. Then there is this "Minimization is heard more with coders using VB.NET then

Get/Set app.config values for WPF and Windows forms using C#

Image
In Windows forms developers sometimes need a place to store information like connection strings or data binding to controls. This is typically done usually under project properties by creating a setting which generates xml e.g. < userSettings >   < WindowsFormsApp1.Properties.Settings >     < setting   name = " Author "   serializeAs = " String " >       < value > Karen Payne </ value >     </ setting >   </ WindowsFormsApp1.Properties.Settings > </ userSettings > Although this works sometimes we want more control so we can have < appSettings >   < add   key = " DatabaseServer "   value = " .\SQLEXPRESS "  />   < add   key = " Catalog "   value = " NorthWind2020 "  />   < add   key = " SortColumn "   value = " CompanyName "  />   < add   key = " SortDirection "   value = " Descending "  /> </ a