Writing C# as a VB.Net programmer

Comparisons have been written before.



VB.Net C# Notes
OrElse || .
Public public Note casing
If public is excluded VB.Net assumes Public. In C# assumes private.
Property MyString As String string MyString;
Property MyString As String var MyString = “Hello World”; In c# var is used to infer
Sub Main static void Main(string[] args)
int + double = double

The model backing the ‘FilmContext’ context has changed since the database was created. Consider using Code First Migrations to update the database

This happened when the database connection string had not been setup in the App.config, so Code First went ahead and created a new database, which I then deleted and set up the connection string to my existing database. I then started getting this error.

Workaround by adding Entity.Database.SetInitializer in the context:

Protected Overrides Sub OnModelCreating(modelBuilder As System.Data.Entity.DbModelBuilder)
Entity.Database.SetInitializer(Of FilmContext)(Nothing) ‘To not initialise the database
modelBuilder.Configurations.Add(New FilmsMap)
End Sub

Entity Framework connection error: System.Data.ProviderIncompatibleException

For me this occurred when I used Entity Framework Power Tools Beta 3, and then used the “Entity Framework > View Entity Data Model (Read-Only)” functionality. The error was:

System.Data.ProviderIncompatibleException: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct. —> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. —> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

This was because the Project containing the context was a class library and was not the startup project. I changed the App.config in the class library but this had no effect. To resolve you change the App.config file in the project marked as the startup project.

Might also be that a reference to the project containing the context is missing.


Dreyfus model of skill acquisition


Instead the original Dreyfus model is based on four binary qualities:
Recollection (non-situational or situational)
Recognition (decomposed or holistic)
Decision (analytical or intuitive)
Awareness (monitoring or absorbed)

This leads to five roles:
1. Novice non-situational recollection, decomposed recognition, analytical decision, monitoring awareness
2. Competence situational recollection, decomposed recognition, analytical decision, monitoring awareness
3. Proficiency situational recollection, holistic recognition, analytical decision, monitoring awareness
4. Expertise situational recollection, holistic recognition, intuitive decision, monitoring awareness
5. Mastery situational recollection, holistic recognition, intuitive decision, absorbed awareness

Get DbContext entities and properties at runtime

The modelbuilder will first build all DbSets specified.
It will then build from any class objects that these DbSets refer to. This is more than you might think. Furthermore on some actions one of these other classes may have not been mapped correctly and cause the main program to crash, even if you were never interested in these classes.

To discover what classes and properties have been built at run-time use the following code:

    Public Sub ProductsContextEntitiesBuiltList()
        Using context As New ProductsContext(ConnectionString.CurrentConnectionString)
        End Using
    End Sub

    Private Sub ContextEntitiesBuiltList(context As Entity.DbContext)
        Dim objContext = CType(context, IObjectContextAdapter).ObjectContext
        Dim workspace As MetadataWorkspace = objContext.MetadataWorkspace
        Dim tables As IEnumerable(Of EntityType) = workspace.GetItems(Of EntityType)(DataSpace.CSpace)
        For Each table In tables

            For Each p In table.Properties
                Debug.WriteLine("   " + p.Name)


        'For Each myEntity In objContext.MetadataWorkspace.GetItems(Of EntityType)(DataSpace.CSpace).ToList
        '    Debug.WriteLine(myEntity.FullName)
    End Sub

Use modelBuilder.Ignore(Of <entity) 'to stop building those not required.

Entity Framework Error “The property ‘id’ is part of the object’s key information and cannot be modified”

If you load an entity then change its key you may get this message.

If you really must change the key, for instance on an entity which does not use identity, then you may set the entity’s state to Added then change the key, then change the state to Unchanged.

To change state you could use

    Sub SetEntityState(myObject As Object, myState As System.Data.Entity.EntityState)
        context.Entry(myObject).State = myState
    End Sub

— Original 8-Jul-2013 —

In my case this was because I was using context.Products.Find(123) and then manipulating the item, perhaps adding it again. So the Find method will cache it the first time. So if you want to do a subsequent Add/Modify then perhaps best to clear that store. You can do this with.

For each p as product in context.products
   context.enty(p).state = detached

It was not that the key was wrong.


Error setting up SQL Server CDC – Change Data Capture

Error when using:
EXECUTE sys.sp_cdc_enable_db

Msg 22830, Level 16, State 1, Procedure sp_cdc_enable_db_internal, Line 193
Could not update the metadata that indicates database <dbname> is enabled for Change Data Capture. The failure occurred when executing the command ‘SetCDCTracked(Value = 1)’. The error returned was 15517: ‘Cannot execute as the database principal because the principal “dbo” does not exist, this type of principal cannot be impersonated, or you do not have permission.’. Use the action and error to determine the cause of the failure and resubmit the request.


Database > Properties > Files >
Check that the databse has an owner

For me this went wrong because ‘dbo’ existed on the server (pc) where this database was created, but did not exist on this server (pc) (SQL Server Developer Edition)

SQL Linked Server Notes (including to an IP)

Create linked server when on the actual server, not as a user on another PC.
If there is a VPN connection required then the VPN connection needs to be up and running on the server, not the local PC.

USE [master]
EXEC master.dbo.sp_dropserver @server=N'', @droplogins='droplogins'
EXEC master.dbo.sp_addlinkedserver @server = N'', @srvproduct=N'SQL Server'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'',@useself=N'False',@locallogin=NULL,@rmtuser=N'RemoteServerLogin',@rmtpassword=''
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'',@useself=N'False',@locallogin='Domain\login',@rmtuser=N'RemoteServerLogin',@rmtpassword=''


@locallogin=NULL means any local login
You cannot use Windows NT group logins, does not work. Individual window users only.

Fix Sony Vaio sound – No speaker or headphones are plugged in

Has happened a couple of times on my Sony Vaio

Diagnosis – This post fixes this issue

  1. Red cross against sound volume in notification tray
  2. Also inspect > Windows Key > Sound >
    • Playback > Only one playback device shown – for me that was “Digital Audio (HDMI) High Definition Audio Device – Not plugged in”
    • Recording > “No Audio Device is installed”
  3. Clicking on the sound icon and asking it to detect and resolve problems does not work.

Solution 1 – Try first because it is quicker and less disruptive

Put in headphones and then click on sound icon in notification tray.
Detecting problems may then resolve the matter and put back in the speakers “Default Device”

Solution 2

In MSConfig find the service or startup that is stopping the sound from working
Possible Catalyst Control Center
Possibly Sony Vaio Event Manager or something of that ilke.

Solution 3

Solved by installing driver from here:


This got my sound back, but I lost my Aero themes. Got this back after just a little more work. Putting following into a FixAero.reg file and installing may have helped. And a second restart after.

Windows Registry Editor Version 5.00

“UseMachineCheck”=dword:00000000 “Blur”=dword:00000000 “Animations”=dword:00000000

Solution 4

Use msconfig and change to Normal Startup will fix it, but lose the Aero theme.


Note: After my laptop started working again, the Program and Features showed that “Realtex High Definition Audio Driver” was installed in the last days.