I'm using Newsoft JSON lib in a vb .net project and I found this error I can't solve.
I use CLSA objects of type Formulario and add some to a list. When I serialize that list the Primary Keys value are fine. In this example the first object of the list has a value of 3 in the ID property but after deserialize the object that value returns 0.
This happend with every ID. I check CSLA class and try to tweak some values like Int32 to Integer or change the readonly property to a read/write property but no avail.
Am I missing something? Every other property has the correct value but the IDs.
Thanks!
Related
I am attempting to migrate a legacy vb.net application to .net standard and turn it into a nuget package. A good amount of it has been straight forward. I am currently hung up on this error caused by functions like this.
Public Property ErrorMessages As Collection
Get
ErrorMessages = _errorMessages
End Get
Set(value As Collection)
_errorMessages = value
End Set
End Property
If i import System.Collections.ObjectModelCollection(Of T) it is asking me for a type and i am unsure how to proceed. It turns my code into
Collection(Of,) and expects a second argument. Has anyone faced this before? Do i use a different import statement or how is this dealt with in vb now?
You should almost certainly replace Collection with Dictionary(Of TKey, TValue), using the dictionary type from the System.Collections.Generic namespace.
Once again, this requires you to fill in the genetic type arguments TKey and TValue with the actual types. You need to figure out from context which type fits the collection. The value of TKey is probably String since that’s the only key type VB6’ collections properly support. And given the name (ErrorMessages), TValue is probably String as well.
VS2013, code first EF6, SQL database, VB
I defined the following classes:
Public Class Question
Public Property QuestionID As Integer
Public Property Text As String
Public Property Type As qType
Public Property PossibleAnswers As New List(Of qAnswer)
End Class
Public Class qAnswer
Public Property qAnswerID As Integer
Public Property Text As String
End Class
When I view the qAnswer data table created in the SQL server I see:
In order to do something so simple as display a list of all answers and their parent questions I need to retrieve the value that is obviously in the table, but I don't understand how to code it. Since it's not technically a property of the class I can't call for it directly. How do I retrieve that value?
I'd like to know if the manner of retrieval will work in both the source code, meaning VB, and the view code, meaning Razor.
Because you created a navigation property to qAnswer on Question, Entity Framework created an implicit foreign key to Question on qAnswer to store the relationship. This results in the Question_QuestionID property you see in your database table. However, since this is an implicit property, there's no way to directly access its value in code. You have two options, and you might want to actually do both.
Add a reference property to qAnswer. If you don't actually care about the related question id, explicitly, and just want to be able to get at the question itself, then you can do so through the reference property. Just add something like the following to qAnswer:
Public Property Question As Question
If you want the id, itself, then you need a property to on qAnswer to store it. Something like:
Public Property QuestionID As Integer
By convention, Entity Framework will recognize this as the foreign key property for the relationship and use it accordingly. You'll need to run a migration after adding this property for it to take effect.
In a solution I have noticed a property that has a type enum:
Public Enum ContentType
HTML = 1
JSON = 2
XML = 3
End Enum
Public Property ContentID() As ContentType
Get
Return _contentID
End Get
Set(ByVal value As ContentType)
_contentID= value
End Set
End Property
Strangely these enums reflect a primary key in a table, I had an issue as a client had different primary keys and this was causing a select statement to not be entered.
Everything else seems to be working and it just got me thinking. My question is will this property throw an error if I try to set the value to be something that isn't contained in the enum? Because as I say this will definitely be happening and I have seen no errors thrown or am I missing something.
will this property throw an error if I try to set the value to be something that isn't contained in the enum?
It will not. Enumerations are backed by an integral type (Integer, Long etc...) and a variable will accept any valid value for its underlying type.
You can use the System.Enum.IsDefined method to check the value before trying to use it:
Returns an indication whether a constant with a specified value exists in a specified enumeration.
In the auto-generated resource designer file, there are properties for each resource. The property calls "GetString" which returns the string value. I would like to override this getstring function so I can do logic to see if I need to retrieve this value or a different value. I can't figure out how to do this because the designer file is auto-generated.
Public ReadOnly Property General() As String
Get
Return ResourceManager.GetString("General", resourceCulture)
End Get
End Property
For example, in my version of the GetString function, I would check the key passed in ("General") and see if there is a custom value for this key in a database. If the custom value exists, I would use that value. If the custom value does not exist, I would call the base GetString function to get the Resource value. I'd like to use the built in Resource class for this because then in my code I can just use "#Resources.General" and take advantage of the auto-complete functionality that already exists.
Refer to ASP.NET Resourcemanager to read local .resx. It's in C# but you can just convert it over. It isn't 100% of what you are looking for but shows a way of overriding in which you may be able to adjust to work with your needs.
I am entirely new to .Net having been working in it for a week at most so please go easy and be detailed as possible :)
I have the following PONO:
Public Class WorkOrderEntity
Private intTrackingNumber As Integer
Private intDateReceived As Integer
Private strManufacturer As String
Public Property TrackingNumber() As Integer
Get
Return intTrackingNumber
End Get
Set(value As Integer)
intTrackingNumber = value
End Set
End Property
Public Property DateReceived() As String
Get
' TODO: Convert timestamp to formatted date
Return intDateReceived
End Get
Set(value As String)
' TODO: Convert formatted date to timestamp
intDateReceived = value
End Set
End Property
End Class
The issue I am faced with is how to store date/time as a timestamp (integer) but provide public properties which format/convert accordingly.
I suppose I could provide a an additional getter()/setter() but ideally I wonder if EF has a way of circumventing this "convention"?
Additionally - I am also curious as to whether it's possible to map properties to columns which are not labelled correctly?
Basically if I were working in a existing database (EF automatically builds my PONO with properties named after table fields) I wish to name the fields something more meaningful; some fields for example might be awkward abbreviations but in the object model I want something more English friendly?
I seem to recall being able to do this with Hibernate in Java (actually it's PHP port) but never the less does EF support such a feature?
Any ideas?
A timestamp SQL Server at least is now a RowVersion. In either case it is not a datetime, but rather a byte array for the version which is auto updated when ever the row changes. You don't convert it to a date.
Regarding mapping an element via configuration rather than convention, you can use the attributes or fluent syntax to specify the mapping. See http://msdn.microsoft.com/en-US/data/jj200620 for info on how to use these features.