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)
            ContextEntitiesBuiltList(context)
        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
            Debug.WriteLine(table.FullName)

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

        Debug.WriteLine("")

        'Or
        'For Each myEntity In objContext.MetadataWorkspace.GetItems(Of EntityType)(DataSpace.CSpace).ToList
        '    Debug.WriteLine(myEntity.FullName)
        'Next
    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
next

It was not that the key was wrong.

 

ASP.Net style css etc.

How to change width of out of box Visual Studio ASP.Net WebForms application

Took a while to work this out. In the Site.Master there is:

<body>
<form runat="server">
<div class="page">...
</div>

If you go to the class=”page” definition on the Site.css you may see a width. This seems to have no effect. I do not know why.

  1. Instead use split view and see on the bottom the different selections e.g.
  2. then with properties showing select
  3. then use the style property
  4. potion
  5. width e.g. 90 %

This may also be achieved in the source on the div tag:

<div class=”page” style=”width: 90%”>

Solved

VB.Net Get FTP list of files

Code:

Imports System.Net

Public Class FTP1

    Private host As String
    Private username As String
    Private password As String

    Sub New(hostValue As String, usernameValue As String, passwordValue As String)
        host = hostValue
        username = usernameValue
        password = passwordValue
    End Sub

    Function GetFileList() As List(Of String)

        Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(host), System.Net.FtpWebRequest)
        ftp.Credentials = New System.Net.NetworkCredential(username, password)
        ftp.KeepAlive = False
        ftp.UseBinary = True
        ftp.Method = WebRequestMethods.Ftp.ListDirectory
        Dim streamReader As New IO.StreamReader(ftp.GetResponse.GetResponseStream)

        Dim result As New List(Of String)
        Dim line = streamReader.ReadLine
        While Not String.IsNullOrEmpty(line)
            result.Add(line)
            line = streamReader.ReadLine
        End While
        Return result

    End Function

End Class

How to copy an ASP.Net web form .aspx

The original page hook up can break

  1. Copy main file
  2. Rename file to remove “Copy of “
  3. View code
  4. Change Public Class name to the new name
  5. Show All Files
  6. Open the <file>.aspx.designer.vb
  7. Rename the class to the new name
    This may clear all errors. If not then continue
  8. If there is code behind then the controls will say “is already declared as..” or “Handle clause requires a WithEvents
  9. View Markup
  10. Change AutoEventWireup=”false” to “true”

 

ASP.Net publish troubleshooting

Was trying to publish to Azure Websites, but it was hanging on “Publish Started”

The Start-up project was another project, so switched it to the website and then published ok. Deliberately switched it back to see if the problem was this and the error returned.

WinForms Designer fails – warning …My.Resources.Resources has no property named … image

SOLUTION from Dave Anson C comment

http://connect.microsoft.com/VisualStudio/feedback/details/540265/designer-fails-warning-my-resources-resources-has-no-property-named

In the project References folder ensure there isn’t a reference to itself. (E.g. You have a project named WindowsApplication1. In the References folder you have a reference to WindowsApplication1)

This worked for me.

Continue Statement (Visual Basic) (T-SQL) “Control-of-Flow Language”

My SQL 70-461 study reminded me about the T-SQL CONTINUE statement to cause execution to jump back to the beginning of the loop. Re. Querying Microsoft SQL Server 2012 – MS Press – p 479

http://msdn.microsoft.com/en-us/library/ms178642.aspx – SQL Server

I keep needing this in VB.Net and for some reason it is missing from my VB.Net vocabulary. Not any longer…

http://msdn.microsoft.com/en-us/library/801hyx6f(v=vs.100).aspx  – VB.Net

Continue transfers control immediately to the next iteration of a loop.

Why Entity Framework is releasing on NuGet only – One Unicorn

Hmm

http://blog.oneunicorn.com/2012/02/11/why-entity-framework-is-releasing-on-nuget-only/

Might work with deployment.

Seems to encourage copying EntityFramework each and every time you copy the files across.

An error occurred generating a bootstrapper: Unable to begin updating resource for bin\Debug\app.publish\setup.exe with error 80070002 TESTPUBLISH

ERROR

An error occurred generating a bootstrapper: Unable to begin updating resource for bin\Debug\app.publish\setup.exe with error 80070002 TESTPUBLISH

A SOLUTION to get it to work

Untick the Prerequisites button

See also:

http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/9ab9b88b-a57b-4945-9f94-e48a82ad5d8c