The entity type is not part of the model for the current context - DBFirst - vb.net

I have a query which selects a list of subrep. The query looks like this:
Public Async Function ReturnSubRepsAsync(user As BaseUser) As Task(Of IList(Of SubRep)) Implements IGeneralAccountManagementRepository.ReturnSubRepsAsync
'The entity type SubRep is not part of the model for the current context.
Using webDataDBContext As New WebDataEntities()
'So here it just has an SQL query which it will execute when requested
Dim subReps = webDataDBContext.Subreps.Where(Function(x) x.CID = user.CID)
If (String.IsNullOrEmpty(user.Password) = False) Then
'Add another where onto the SQL query
subReps = subReps.Where(Function(x) x.HASH_PASSWORD = user.PasswordHash)
End If
If (String.IsNullOrEmpty(user.Username) = False) Then
'Add another where onto the SQL query
subReps = subReps.Where(Function(x) x.username = user.Username)
End If
'Execute the query
Return Await subReps.ToListAsync()
End Using
End Function
Now due to entity framework database not including validation such as MaxLength on strings, I decided to follow this tutorial: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation
And add another partial public class which uses a MetaDataType to implement the validation. Now during debugging, I thought it was an issue with a column so I decided to comment out all of the code inside my separate partial class to test if it works, so I'm not left with an empty husk of a partial class. It has not made a difference, it still errors on the first where of my query.
My entity framework generated class looks like this:
Partial Public Class Subrep
Public Property id As Integer
Public Property CID As String
Public Property username As String
Public Property LastName As String
Public Property FirstName As String
Public Property MIddleInitial As String
Public Property Address As String
Public Property Address2 As String
Public Property City As String
Public Property State As String
Public Property Zip As String
Public Property HomePhone As String
Public Property EmailAddress As String
Public Property InActive As String
Public Property CellPhone As String
Public Property StartDate As String
Public Property AuditName As String
Public Property Background As String
Public Property FDCPA As String
Public Property Choicepoint As String
Public Property Badge As String
Public Property ConfAgree As String
Public Property NCCI_OK As String
Public Property InactiveRepNo As String
Public Property Approved As String
Public Property DateApproved As String
Public Property Denied As String
Public Property DateDenied As String
Public Property DeniedNotes As String
Public Property DOB As String
Public Property SSN As String
Public Property MegansLaw As String
Public Property MASTER As String
Public Property VACATION As String
Public Property FC_DEFAULT_FEE As Nullable(Of Decimal)
Public Property LM_DEFAULT_FEE As Nullable(Of Decimal)
Public Property MAS90 As String
Public Property SOS As String
Public Property HASH_PASSWORD As String
Public Property changepass As String
Public Property NOTIFICATION_OFF As String
Public Property Capacity As String
Public Property internalqc As String
Public Property New_Rep As String
Public Property Stand_By As String
Public Property Vacation_Start_date As Nullable(Of Date)
Public Property Vacation_End_date As Nullable(Of Date)
Public Property sub_coreIP As String
Public Property rating As String
Public Property date_last_updated As Nullable(Of Date)
End Class
And my separate partial class which is supposed to be used for validation looks like this:
Partial Public Class SubRep
End Class
So even though it's empty I get the error listed in the title. I have gotten this to work with multiple entities perfectly fine.

Just wow.
So with visual studio intellisense if you press F12 to try to find references it highlighted both partial classes Subrep and SubRep so that would lead you to believe that both were associated with one another. Turns out that because of the uppercase R in the second partial class SubRep, it's not associated with the model and therefore needs to match the case of the entity Subrep. Like this:
Partial Public Class Subrep
End Class
What's even weirder is that the Property in my DBContext uses an uppercase SubRep. Like this:
Public Overridable Property Subreps() As DbSet(Of SubRep)

Related

How to parse this JSON string properly

I have some API That returns the following JASON, and im trying to parse it to be able to pull from any attributes i want.
JSON im trying to parse: https://pastebin.com/i06GRD0f
I'm assuming the proper way would be to load everything into an array but i can't get it to work right as it's a little confusing with all different parameters.
Imports System.Web.Script.Serialization
Public Class ServerAttributes
Public Property assists() As String
Public Property bestRankPoint() As String
Public Property boosts() As String
Public Property dBNOs() As String
Public Property dailyKills() As String
Public Property dailyWins() As String
Public Property damageDealt() As String
Public Property days() As String
Public Property headshotKills() As String
Public Property heals() As String
Public Property killPoints() As String
Public Property kills() As String
Public Property longestKill() As String
Public Property longestTimeSurvived() As String
Public Property losses() As String
Public Property maxKillStreaks() As String
Public Property mostSurvivalTime() As String
Public Property rankPoints() As String
Public Property rankPointsTitle() As String
Public Property revives() As String
Public Property rideDistance() As String
Public Property roadKills() As String
Public Property roundMostKills() As String
Public Property roundsPlayed() As String
Public Property suicides() As String
Public Property swimDistance() As String
Public Property teamKills() As String
Public Property timeSurvived() As String
Public Property top10s() As String
Public Property vehicleDestroys() As String
Public Property walkDistance() As String
Public Property weaponsAcquired() As String
Public Property weeklyKills() As String
Public Property weeklyWins() As String
Public Property winPoints() As String
Public Property wins() As String
End Class
Public Class ServerData
Public Property data() As ServerAttributes
End Class
Module StaterMainClass
Public Sub GetInfo()
Dim jss As New JavaScriptSerializer()
Dim DataResponse As ServerData = jss.Deserialize(Of ServerData)(JSON)
Debug.Print(DataResponse.data.assists(0))
End Sub
End Module

Serializer with VB.net

my question is how serializer the next string Json:
data:
{
"Customers":[{"**PhoneNumber**":"123123123"},{"**PhoneNumber**":"321321321"}],
"Expand":false,
"Include":false
}
I to have doubt, because in the string JSon I have 2 equal labels, therefore i not know as create my class with the same labels, i'm using VB.net.
My Classes in VB.net
Public Class Customers
Public as customer1
Public expand as boolean
Public include as boolean
End Class
Public Class customers1
Public PhoneNumber as String
End Class
Having the same field duplicated in this instance is okay because it is in a collection/array. In this JSON example you have two Customer objects for the Response's Customers collection.
Your object model would look like this:
Public Class Response
Public Property Customers As List(Of Customer)
Public Property Expand As Boolean
Public Property Include As Boolean
End Class
Public Class Customer
Public Property PhoneNumber As String
End Class
An example of deserializing it:
Dim objSerializer As New JavaScriptSerializer()
Dim objResponse As Response = objSerializer.Deserialize(Of Response)(strData)

DropDownListFor using List of Class

I cant seem to get my HTML.DropDownListFor() to work properly with my structures.
CategoriesEditViewModel
Public Class CategoriesEditViewModel
Public Property Categories As List(Of cihCategoryOrgDef)
Public Property Category As cihCategoryOrgDef
Public Property lists As cihLists = New cihLists()
Public Property SelectedItem As String
Public Sub New(catId as Guid)
SelectedItem = codId.ToString
lists.loadClubWaivers(ZenCommon.CurrentOrgId)
End Sub
End Class
cihLists
Private orgWaiverList As List(of cihWaiver)
Public Sub loadClubWaivers(orgId as Guid)
'Go to database and populate orgWaiverList
End Sub
Public ReadOnly Property organizationClubWaivers() As List(Of cihWaiver)
Get
Return orgWaiverList
End Get
End Property
cihWaiver
Public Class cihWaiver
Public waiverId As Guid = Guid.Empty
Public waiverName As String = ""
Public waiverText As String = ""
End Class
Edit View Page
#Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverText", Model.lists.organizationClubWaivers))
The error i get is 'Library.cihWaiver' does not contain a property with the name 'waiverId'.
but the cihWaiver class clearly has an item for 'waiverId'. I haven't done MVC stuff in a while, so maybe i'm going about this all wrong.
The answer was as simple as changing my cihWaiver Class to this:
Public Class cihWaiver
Public Property waiverId As Guid = Guid.Empty
Public Property waiverName As String = ""
Public waiverText As String = ""
End Class
cane someone explain to me why the keyword Property is so important?

How to iterate through every property in an every Object in my list of Objects vb

So I have a list of Objects and I want to get every property in every object and save it as a string.
Object looks like
Public Class parameters
Public Property test As String
Public Property test_type As String
Public Property user_test_name As String
Public Property meas As String
Public Property spec_min As String
Public Property spec_max As String
Public Property spec_unit As String
Public Overrides Function ToString() As String
Return user_test_name
End Function
End Class
This is what i have so far
For Each parameters In param
Dim myType As Type = GetType(parameters)
Dim properties As System.Reflection.PropertyInfo() = myType.GetProperties()
For Each p As System.Reflection.PropertyInfo In properties
MsgBox(p.Name)
Next
Next

ValueInjecter and ICollection(Of ViewModel)

I have two simple viewmodels which are related. I can query the data via an linq include statement, but when i inject it into the viewmodel the icollection is nothing?
Viewmodels:
Public Class EventViewModel
Public Property EVENTID As Integer
Public Property TITLE As String
Public Overridable Property USERS() As ICollection(Of UserViewmodel)
End Class
Public Class UserViewModel
Public Property USERID As Integer
Public Property EVENTID As Integer
Public Property NAME As String
End Class
Query:
Dim dataObject As EVENTTABLE = db.EVENTTABLE.Include("USERTABLE").Single(Function(c) c.EVENTID= "1")
Users are in the Object!
Injection:
viewModel.InjectFrom(dataObject)
Users are Nothing?
Ok figured it out with help from this question: How to map lists with ValueInjector
I needed to query the USERTABLE into a list and inject it like stated below:
Query:
Dim dataSubObject = (From ....).toList()
Injection:
Dim viewModel = New EventViewModel
viewModel.USERS= dataSubObject.Select(Function(x) New ImageViewModel().InjectFrom(x)).Cast(Of ImageViewModel)().ToList()