I am using Linq to Sql that generated a data contract for a table. I have a date field in that table which is a non-nullable field. I need to override the auto generated property of the date field to return a specific value, something like
Get
if_dt<>date.minvalue
return _dt
else
return string.empty
End Get
Is it possible to override an autogenerated property in the designer.vb file using a partial class? I dont want to create a new property as it is currently being accessed in n number of places and I dont want to change it in every place.
No, you'll need to modify the .designer file or inherit from it and change the behavior of that property (but I guess the auto-generated property won't be virtual so you'll need to edit it anyway)
Related
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.
All the examples I am finding for using the AliasToBean transformer use the sessions CreateSqlQuery method rather than the CreateQuery method. They also only return the basic value types, and not any object's of the existing mapped types.
I was hoping it would be possible that my DTO have a property of one of my mapped Domain objects, like below, but I am not getting traction. I get the following exception:
Could not find a setter for property '0' in class 'namespace.DtoClass'
My select looks like the following on my mapped classes (I have confirmed the mappings pull correctly):
SELECT
fcs.MeasurementPoint,
fcs.Form,
fcs.MeasurementPoint.IsUnscheduled as ""IsVisitUnscheduled"",
fcs.MultipleEntryAllowed
FROM FormCollectionSchedule fcs
My end query will be more complex, but I wanted to confirm if this AliasToBean method can return mapped domain objects as well as basic field values from tables retrieved via sql.
the query execution looks like the following:
var result = session.CreateQuery(hqlQuery.ToString())
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof (VisitFormCollectionResult)))
.List<VisitFormCollectionResult>();
note: the VisitFormCollectionResult DTO has more properties, but I wanted to know if I could populate the domain object properties matching the names
update found my problem! I have to explicitly alias each of the fields. once I added an alias, even though the member property on the class matched my DTO's property name, the hydration of the object worked correctly.
The answer to my own question was that each of the individual fields in the select needed an explicit alias matching the property, regardless if the field name already matched the property name of the DTO object:
SELECT
fcs.MeasurementPoint as "MeasurementPoint",
fcs.Form as "Form",
fcs.MeasurementPoint.IsUnscheduled as "IsVisitUnscheduled",
fcs.MultipleEntryAllowed as "MultipleEntryAllowed"
FROM FormCollectionSchedule fcs
I have a (Serializable) class (Accounts), and inside this class some properties, including a Dictionary of another Serializable class (Symbols).
I Serialize the class (Accounts) and save them into a file to be used for next run.
Now, I have a new version of the application, which contain a new properties inside (Symbols) class. When I Deserialize the (Accounts), it loads the Dictionary correctly, but with un-desired values (the strings are nothing and the booleans are false).
Can I set a default values for those new properties inside the Symbols class when I deserialize? Note that I want this without a For loop, since the Dictionary of Accounts and the Symbols are large, which means I will need a huge double for loop to solve this using for loop.
Thanks.
Ahmad
You can just specify a default value for each property:
For backing fields:
Private _someProperty as Boolean = True
For auto-Implemented Properties:
Public Property SomeProperty As Boolean = True
I managed to work-around this case by making all my variables as Strings (to be handled as objects), and within my code, I check if the value is Nothing, then I will assign the default value when needed... I know this is not the best solution, but at least it worked out!
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.