I'm learning asp.net MVC. Using the Code First model, where you create your class, and then use the EF to manage all database interactions.
Is there anyway of only requesting some of the fields from a given table using this method, and only updating those fields?
My whole class is:
Public Class Employee
Public Property ID() As Integer
<DisplayName("Staff Name")>
<Required()>
Public Property StaffName() As String
<DisplayName("Location")>
<Required()>
Public Property Location() As String
<Required()>
<DisplayName("Team Leader")>
Public Property teamleader() As String
Public Property ScoreCardM1() As Integer
Public Property ScoreCardM1Notes() As String
Public Property ScoreCardM2() As Integer
Public Property ScoreCardM2Notes() As String
Public Property ScoreCardM3() As Integer
Public Property ScoreCardM3Notes() As String
Public Property ScoreCardM4() As Integer
Public Property ScoreCardM4Notes() As String
Public Property ScoreCardM5() As Integer
Public Property ScoreCardM5Notes() As String
Public Property ScoreCardM6() As Integer
Public Property ScoreCardM6Notes() As String
Public Property ScoreCardTotal() As Integer
Public Property ScoreCardTotalNotes() As String
Public Property ScoreCardM7() As Integer
Public Property ScoreCardM7Notes() As String
Public Property ScoreCardM8() As Integer
Public Property ScoreCardM8Notes() As String
Public Property ScoreCardM9() As Integer
Public Property ScoreCardM9Notes() As String
Public Property ScoreCardQ3Total() As Integer
Public Property ScoreCardQ3TotalNotes() As String
Public Property ScoreCardM10() As Integer
Public Property ScoreCardM10Notes() As String
Public Property ScoreCardM11() As Integer
Public Property ScoreCardM11Notes() As String
Public Property ScoreCardM12() As Integer
Public Property ScoreCardM12Notes() As String
Public Property ScoreCardQ4Total() As Integer
Public Property ScoreCardQ4TotalNotes() As String
Public Property GeneralNotes() As String
End Class
However, I only want to display and edit one month at a time:
Public Class Employee
Public Property ID() As Integer
<DisplayName("Staff Name")>
<Required()>
Public Property StaffName() As String
Public Property ScoreCardM1() As Integer
Public Property ScoreCardM1Notes() As String
End Class
How do I best achieve this? Do I setup another 12 classes, with just one month contained within each, or is there a best practice way of updating a subset of fields within a database row, without affecting the others?
Thanks for any help,
Mark
further to my comment above.... you can set up your classes as follow:
Employee Class:
Public Class Employee
Public Property EmployeeID() As Integer
<DisplayName("Staff Name")>
<Required()>
Public Property StaffName() As String
<DisplayName("Location")>
<Required()>
Public Property Location() As String
<Required()>
<DisplayName("Team Leader")>
Public Property teamleader() As String
Public virtual Property reports() As ICollection<Of MonthlyRports> // not sure if the syntax is right (haven't worked in VB)
End Class
MonthlyReport Class
Public Class Employee
Public Property MonthlyReportID() As Integer // primary key
Public Property EmployeeID() As Integer // foreign key. who this report belongs to
Public Property Month As String // report for month ...
Public Property ScoreCard() As Integer
Public Property ScoreCardNotes() As String
End Class
Related
I have a class mapped to an SQL table and can perform a Linq query ok. I need to access some of the information in Products and modify data such as Price before returning as a JSon object.
The problem is a i cant access any of the information in Products. Could someone please advise how i could achieve this?
Class:
<Table(Name:="dbo.VXPartsData")>
Public Class VXPartsData
<Key()>
Public Property PartNo() As String
Public Property CustPart() As String
Public Property ShortDesc() As String
Public Property Part() As String
Public Property Price() As Decimal?
Public Property DiscCode() As String
Public Property Kit() As Boolean
Public Property VXCODE() As String
End Class
Linq query:
Dim Products = DB.VXPartsData.Where(Function(e) e.PartNo = pair.Key).ToList()
I'm looking to add a navigational property to my 'Registration' class, so I can access the parent 'Person'.
Ideally, I would be able to add Public Overrideable Property ParentPerson as Person, but I can't seem to get this to work as it complains about the principle end of association.
Public Class Person
<Required()>
Public Property ID As Integer
<Required()>
<StringLength(150)>
Public Property Firstname As String
<Required()>
<StringLength(150)>
Public Property Lastname As String
Public Overridable Property Registration As Registration
End Class
Public Class Registration
<Required()>
Public Property ID As Integer
Public Property RegistrationDate As Date
Public Overridable Property Sessions As List(Of RegistrationSession)
End Class
I have two entities linked with an association entity. In code I can easily insert one, or all of these at once. But, on the front end, I am using FormViews to allow the user to enter data. When I insert an address, how do I let it know to insert a facility at the same time, unless I have the facility and the address on the same formview?
Public Class facility
<Key()> _
Public Property facility_id As Integer
Public Property facility_present_use As String
Public Property facility_prior_use As String
Public Property facility_occupied As Boolean
Public Property facility_size As Double
Public Property facility_floors As Integer
Public Property facility_age As Integer
Public Property facility_single_residence As Boolean
Public Property facility_number_of_units As Integer
Public Overridable Property facility_address As ICollection(Of facility_address) = New HashSet(Of facility_address)
End Class
Public Class address
<Key()> _
Public Property address_id As Integer
Public Property address_name As String
Public Property address_address_1 As String
Public Property address_address_2 As String
Public Property address_city As String
Public Property address_state As Integer?
Public Property address_zip As String
Public Property address_contact_first_name As String
Public Property address_contact_last_name As String
Public Property address_phone_area As String
Public Property address_phone As String
Public Overridable Property lu_state As lu_state
Public Overridable Property facility_address As ICollection(Of facility_address) = New HashSet(Of facility_address)
End Class
Public Class facility_address
<Key()> _
Public Property facility_address_id As Integer
Public Property address_type As Integer
Public Property facility_id As Integer
Public Property address_id As Integer
Public Overridable Property lu_address_type As lu_address_type
Public Overridable Property facility As facility
Public Overridable Property address As address
End Class
<HttpGet()>
Public Function Search(<FromUri()> ByVal name As Name) As HttpResponseMessage
// get params from complex type
// or check for model validation
name.firstName;
name.lastName;
End Function
Public Class Name
<Required()>
Public firstName As String
<Required()>
Public lastName As String
End Class
/api/abc/search?firstName=jack&lastName=daniels
I am trying to send a comlex type as a query parameter but name is always null even though I am using fromUri attribute. What am I missing?
EDIT: I am also using Required() attribute from System.ComponentModel.DataAnnotations .
I found the problem. I was missing the Property keywords on my fields.
Public Class Name
<Required()>
Public firstName As String
<Required()>
Public lastName As String
End Class
So, it worked with the following change.
Public Class Name
<Required()>
Public Property firstName As String
<Required()>
Public Property lastName As String
End Class
The problem solved. I think, without properties, the class does not expose its fields, so I cant read them from uri.
I have a .NET 2010 project. In it, I create a basic user class. Instantiate it and successfully fill it with data. The entire time it is doing this, if I hover over the class, it says it is nothing, even as it fills its properties. Later on, it hoses me in the UI, even though the property has a value it says the class is nothing.
I DO instatiate the class...
Dim oExtendedUser As New ExtendedUser
and here is the classs definition...
Public Class ExtendedUser
Inherits System.Web.Security.MembershipUser
Public Sub New()
_Role = New Role
End Sub
Public Property ExtendedUserID As Int32
Public Property FirstName As String
Public Property LastName As String
Public Property Phone As String
Public Property UserID As Guid
Public Property Role As Role
Public Property UserName() As String
Public Property Password() As String
Public Property SecurityQuestion() As String
Public Property SecurityAnswer() As String
End Class
I changed the class. I added MyBase.New() but the problem persists. On the UI, here is the code that executes when the button is clicked. Director has, as a proprety, ExtendedUSer
Dim oCase As New BE.Case
Dim oDirector As New BE.Director
oDirector = SessionManager.Director 'the values are here
oCase.Investigator.ExtendedUserID = oDirector.ExtendedUser.ExtendedUserID
And here is the Director...
Public Class Director
Public Sub New()
_ExtendedUser = New ExtendedUser
End Sub
Public Property ID As Int32
Public Property ExtendedUser As ExtendedUser
End Class
You got tricked by the ToString Override.
Your object exists but it overrides the ToString Method : MembershipUser.ToString
To validate this behavior, try it with a simple class :
VB.NET
Public Class Test
Public Property TestString As String
Public Overrides Function ToString() As String
Return Me.TestString
End Function
End Class
C#
public class Test
{
public string TestString { get; set; }
public override string ToString()
{
return this.TestString;
}
}
With this code, the Watch will show you an instanciated Test to Nothing, because ToString value will be Nothing. The object exist, but Visual Studio is using the ToString Method to populate the value field, and at this point it is Nothing.
VB.NET
Public Class Test
Public Property TestString As String = ""
Public Overrides Function ToString() As String
Return Me.TestString
End Function
End Class
C#
public class Test
{
public Test()
{
this.TestString = "";
}
public string TestString { get; set; }
public override string ToString()
{
return this.TestString;
}
}
With this code you'll get an empty string.
To get back to your code, you cannot extend the MembershipUser so simply, you have to follow this guideline : How to: Implement a Custom Membership User. As many things won't work with your actual extension (For example, your username shadowing the base one).
See this question as well. There are easier ways to extend your user "entity" than inheritance.