Come aggiorno una riga SQL a cui è stato assegnato un ID con LINQ to SQL?
-
06-07-2019 - |
Domanda
Dato questo schema:
Fruits
- FruitID INT PK
- FruitName NVARCHAR(30)
- FruitStatusID INT NULL FK: Statuses
Statuses
- StatusID INT PK
- StatusName NVARCHAR(30)
Se ho in mano un FruitID
(solo un int, non un oggetto Fruit), come posso aggiornare FruitName
e annullare l'annullamento di FruitStatusID senza caricare prima l'oggetto Fruit dal database?
Nota: questa soluzione mi porta abbastanza lontano, ma non riesco a capire come annullare una colonna FK.
Risposte in C # o VB, grazie!
Soluzione
Funziona ma sembra inutilmente complicato:
''//initialize the values I'm going to null out to something
Dim Tag As Data_Tag = New Data_Tag() With {
.Data_Tag_ID = DataTagID,
.Last_Error_DateTime = New DateTime(),
.Last_Error_Message = "",
.Last_Error_Severity_Type_ID = -1 }
''//start change tracking
DB.Data_Tags.Attach(Tag)
''//record changes to these properties (must be initialized above)
Tag.Last_Error_DateTime = Nothing
Tag.Last_Error_Message = Nothing
Tag.Last_Error_Severity_Type_ID = Nothing
DB.SubmitChanges()
Sicuramente c'è un modo migliore!
(nota: la strana sintassi dei commenti è solo per il codice highliger - non gli piacciono i commenti in stile VB)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow