Copy data from screen to a subtable with relationship
-
2012년 4월 12일 목요일 오후 8:52
Hi
I have a table Person and a table Label. Based on a Search Screen I want to copy the Persons (Person ID) data within this screen to another table (Label) with a button. The goal of this is to finally print labels from the table Label. The table Label looks like this:
La_Id (Identity, Primary Key, auto value)
La_PeId (Foreign Key to Person, Person ID)
La_User (name of the user who copy the data)
La_CDate (date of copy)
The following code works so far:Private Sub LabelsCopy_Execute()
Dim newLabel As Label = DataWorkspace.ApplicationData.Labels.AddNew()
With newLabel
.La_User = Me.Application.User.Name()
.La_CDate = Date.Now()
End With
Me.DataWorkspace.ApplicationData.SaveChanges()
End SubHere my questions:
- How can I add the relevant Person ID's (foreign Key) to the table Label? I cannot add data to the property La_PeId, for example .La_PeId = 67
- How can I copy all the records (Person ID's) from the search screen to the table Label?
I hope you understand my problem. Thanks in advance for your help.
Regards,
Thomas
모든 응답
-
2012년 4월 14일 토요일 오전 5:59
I think I understand what you're trying to do. You cannot assign to .La_PeId = 67 because LS doesn't work that way. Lightswith and entity framework together provide you access the Person entity associated to the Label Entity. From there you derive the PersonId directly from the relationship such as this.Label.Person.PeId
To set the PeId you must set the Person property of label. I'm assuming the that you are clicking a button on a row in the Person grid in the Search screen? Try this
-------------------------------------------------------------------------------------------------------------------------------
Dim newLabel As Label = DataWorkspace.ApplicationData.Labels.AddNew()
With newLabel
.La_User = Me.Application.User.Name()
.La_CDate = Date.Now()
.Person = this.Person.selectedItem()
End With
This is the same but for use if you're not in the person screen and not able to access a selected person
------------------------------------------------------------------------------------------------------------------------
Dim newPerson As Person= DataWorkspace.ApplicationData.Person.GetFirstOrDefault(PeId)Dim newLabel As Label = DataWorkspace.ApplicationData.Labels.AddNew()
With newLabel
.La_User = Me.Application.User.Name()
.La_CDate = Date.Now()
.Person = newPerson
End WithNow hopefully that answer makes sense for your question :)
MS Developer Guy
- 답변으로 제안됨 Dino HeModerator 2012년 4월 17일 화요일 오전 2:47
- 답변으로 표시됨 Thomas Hs 2012년 4월 17일 화요일 오전 8:06
-
2012년 4월 17일 화요일 오전 8:05
Hi
Thanks a lot for your help - works perfect. You saved my day! And I hopefully understand better the entity concept within Lightswitch.
Regards,
Thomas

