Question how to save record from entity model

navagomeza

Member
Joined
Jan 13, 2014
Messages
12
Programming Experience
1-3
I am finding a record in the entity model, by using a DataKeyName that contains the id of the record.

I want to be able to save the record in a variable to save all the values in a max column in a data table.

This what I have so far:

Dim myContext As New OLTPEntities
Dim myRecipeRecord = myContext.RefineRecipe.Where("it.Id=" & Me.dvRecipeItem.DataKey(0).ToString()).ToList()


and then to save the record:
Dim myAppLog As New AppLogClass(inNote:=myRecipeRecord.ToString())
myAppLog.Save()

But after gets saved, and I check the table, what I see in the column is:
System.Collections.Generic.List`1[Recipe.RefineRecipe]


What I want to see in the column is the record as a string containing all the values. What am I missing here?

Please any help would be great.

Thank you.
 
If you're only getting one record then why call ToList, which obviously creates a list of objects? If you want to get a single record from an EF query then you call First, or FirstOrDefault if there's any possibility that there may not be a matching record. If you call FirstOrDefault then you must first test that the result is not Nothing, i.e. there was no matching record, and then you can get the appropriate property value from the record, not call its ToString method to convert the record itself to a String.
 
Hello,

This is the best way I have been able to figure it out:

Dim myRecipeRecord = myContext.RefineRecipe.Where("it.Id=" & Me.dvRecipeItem.DataKey(0).ToString()).FirstOrDefault
Dim strRefineRecipeRecord = New StringBuilder("")


strRefineRecipeRecord.Append("Id= " & myRecipeRecord.Id & ", ")
strRefineRecipeRecord.Append("ItemNumber= " & myRecipeRecord.ItemNumber & ", ")
strRefineRecipeRecord.Append("VersionNumber= " & myRecipeRecord.VersionNumber & ", ")
strRefineRecipeRecord.Append("ModifiedDateTime " & myRecipeRecord.ModifiedDateTime & ", ")
strRefineRecipeRecord.Append("ModifiedBy= " & myRecipeRecord.ModifiedBy & ", ")
strRefineRecipeRecord.Append("MeltPointLow= " & myRecipeRecord.MeltPointLow & ", ")
strRefineRecipeRecord.Append("MeltPointHigh= " & myRecipeRecord.MeltPointHigh & ", ")
strRefineRecipeRecord.Append("FinenessLow= " & myRecipeRecord.FinenessLow & ", ")
strRefineRecipeRecord.Append("FinenessHigh= " & myRecipeRecord.FinenessHigh & ", ")
strRefineRecipeRecord.Append("ViscosityLow= " & myRecipeRecord.ViscosityLow & ", ")
strRefineRecipeRecord.Append("ViscosityHigh= " & myRecipeRecord.ViscosityHigh & ", ")
strRefineRecipeRecord.Append("ViscosityMethodCode= " & myRecipeRecord.ViscosityMethodCode & ", ")
strRefineRecipeRecord.Append("ProcessMinutesMax_Legacy= " & myRecipeRecord.ProcessMinutesMax_Legacy & ", ")
strRefineRecipeRecord.Append("EmptyGateRate= " & myRecipeRecord.EmptyGateRate & ", ")
strRefineRecipeRecord.Append("BulkTankTemp= " & myRecipeRecord.BulkTankTemp & ", ")
strRefineRecipeRecord.Append("ProcessStartPosition= " & myRecipeRecord.ProcessStartPosition & ", ")
strRefineRecipeRecord.Append("ProcessLineType= " & myRecipeRecord.ProcessLineType & ", ")
strRefineRecipeRecord.Append("SpecDensity= " & myRecipeRecord.SpecDensity & ", ")
strRefineRecipeRecord.Append("MixProcessTime= " + myRecipeRecord.MixProcessTime.ToString() + ", ")
strRefineRecipeRecord.Append("MixProcessCount= " & myRecipeRecord.MixProcessCount & ", ")
strRefineRecipeRecord.Append("MixSpeed= " & myRecipeRecord.MixSpeed & ", ")
strRefineRecipeRecord.Append("AgitatorEmptySpeed= " & myRecipeRecord.AgitatorEmptySpeed & ", ")
strRefineRecipeRecord.Append("MixerTemp= " & myRecipeRecord.MixerTemp & ", ")
strRefineRecipeRecord.Append("CalculatedFat= " & myRecipeRecord.CalculatedFat & ", ")
strRefineRecipeRecord.Append("FatSpecLow= " & myRecipeRecord.FatSpecLow & ", ")
strRefineRecipeRecord.Append("FatSpecHigh= " & myRecipeRecord.FatSpecHigh & ", ")
strRefineRecipeRecord.Append("FatAdjustLow= " & myRecipeRecord.FatAdjustLow & ", ")
strRefineRecipeRecord.Append("FatAdjustHigh= " & myRecipeRecord.FatAdjustHigh & ", ")
strRefineRecipeRecord.Append("ConcheRecipeNumber= " & myRecipeRecord.ConcheRecipeNumber & ", ")
strRefineRecipeRecord.Append("RefinerRecipeNumber= " & myRecipeRecord.RefinerRecipeNumber & ", ")
strRefineRecipeRecord.Append("PreRefinerRecipeNumber= " & myRecipeRecord.PreRefinerRecipeNumber & ", ")
strRefineRecipeRecord.Append("TargetRecipeWeight= " & myRecipeRecord.TargetRecipeWeight & ", ")
strRefineRecipeRecord.Append("RecipeWeight= " & myRecipeRecord.RecipeWeight & ", ")
strRefineRecipeRecord.Append("ColorLow= " & myRecipeRecord.ColorLow & ", ")
strRefineRecipeRecord.Append("ColorHigh= " & myRecipeRecord.ColorHigh)


Does this look like a good way of doping it? I would like to follow what the standard is. Please advice.

Thank you very much.
 
I have added a Not IsNothing check before running the string builder code. And I removed the ToString() from the FirstOrDefault.
I am not ignoring your recommendation in purpose, it just slipped my sight, and I am not very much experienced.

Thank you
 
I have added a Not IsNothing check before running the string builder code.

It would be more correct to use:
If myRecipeRecord IsNot Nothing Then
 
Got it.

Is it possible to access the original value and modified value of the record?

I would like to be able to save an audit of what values where modified, and pass those to the string builder to save in the history table. Or whichever is a more efficient way

So far the code with your recommendations, I removed some columns to keep it short:
Protected Sub dvRecipeItem_ItemCommandEventHandler(sender As [Object], e As DetailsViewCommandEventArgs)


        Dim myContext As New OLTPEntities
        Dim myRecipeRecord = myContext.RefineRecipe.Where("it.Id=" & Me.dvRecipeItem.DataKey(0)).FirstOrDefault()
        Dim strRefineRecipeRecord = New StringBuilder("")


        If myRecipeRecord IsNot Nothing Then
            strRefineRecipeRecord.Append("Id= " & myRecipeRecord.Id & ", ")
            strRefineRecipeRecord.Append("ItemNumber= " & myRecipeRecord.ItemNumber & ", ")
            strRefineRecipeRecord.Append("ColorLow= " & myRecipeRecord.ColorLow & ", ")
            strRefineRecipeRecord.Append("ColorHigh= " & myRecipeRecord.ColorHigh)
        End If


        If e.CommandName = "Update" Then
            If Not IsNothing(Me.dvRecipeItem.DataKey(0).ToString()) Then
                ' Log Update
                Dim strId As String = Me.dvRecipeItem.DataKey(0).ToString()
                Dim strModifiedBy As String = System.Web.Security.Membership.GetUser.UserName.Trim
                Dim myAppLog As New AppLogClass(inDescription:="Item Updated By: " & strModifiedBy, inNote:=strRefineRecipeRecord.ToString())
                myAppLog.Save()
            End If
        End If
    End Sub
 
Back
Top