简体   繁体   中英

Entity Framework inserting wrong data?

I have a piece of code where I am updating a record in my database, copying that object using ValueInjector and then attempting to add that new object to my database using Entity framework:

//Get record to update
File currentFile = db.Files.First(t => t.FileId == updatedFile.FileId && t.CustomerId == PrimaryUser);
currentFile.ToLanguage = 2; //update that record
File copy = Helpers.CopyFile(currentFile); //copy the currentFile
copy.FileId = Guid.NewGuid();
copy.ToLanguage = 3;
copy.Project = null;
db.Files.Add(copy); //add new record to the database
db.SaveChanges();

Definition for CopyFile:

public static VerbalInk.Data.File CopyFile(Data.File source)
{
    Data.File newFile = new Data.File();
    newFile.InjectFrom(source);
    return newFile;
}

When I place a breakpoint at my db.Files.Add(copy); line and then view copy.ToLanguage it has the expected value of 3 , but for some reason when I save my changes to the database both records have a value of 2 for ToLanguage .

Does anyone have any idea why this would be happening and how I can get the correct value for ToLanguage inserted?

UPDATE:

I have found that if I disable Lazy Loading for my data context using the line below:

db.Configuration.LazyLoadingEnabled = false;

then everything updates as expected. Can someone explain why this would happen? Is there a way I can get my code working without disabling lazy loading?

File definition:

public partial class File
{
    public File()
    {
        this.FileServices = new HashSet<FileService>();
        this.FileAudioVariables = new HashSet<FileAudioVariable>();
        this.FileLanguages = new HashSet<FileLanguage>();
    }

    public System.Guid FileId { get; set; }
    public System.Guid CustomerId { get; set; }
    public Nullable<System.Guid> TemplateId { get; set; }
    public Nullable<System.Guid> OrderId { get; set; }
    public string FileName { get; set; }
    public bool IsMailInFile { get; set; }
    public string FileUrl { get; set; }
    public Nullable<int> NumberOfSpeakers { get; set; }
    public int AudioLength { get; set; }
    public int BillableMinutes { get; set; }
    public Nullable<int> StatusId { get; set; }
    public string Notes { get; set; }
    public string VirtualType { get; set; }
    public System.DateTime CreateDate { get; set; }
    public System.DateTime UploadDate { get; set; }
    public bool Tracked { get; set; }
    public bool Exported { get; set; }
    public Nullable<bool> IsConverting { get; set; }
    public Nullable<bool> IsInfected { get; set; }
    public Nullable<System.DateTime> OrderDate { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> DueDate { get; set; }
    public Nullable<System.Guid> TranscriptionistId { get; set; }
    public string TranscriptionistNotes { get; set; }
    public int BillingStatusId { get; set; }
    public Nullable<System.Guid> ProjectId { get; set; }
    public Nullable<bool> Specialized { get; set; }
    public Nullable<int> TurnaroundId { get; set; }
    public Nullable<int> FromLanguage { get; set; }
    public Nullable<int> ToLanguage { get; set; }
    public Nullable<System.Guid> ParentFileId { get; set; }

    public virtual ICollection<FileService> FileServices { get; set; }
    public virtual ICollection<FileAudioVariable> FileAudioVariables { get; set; }
    public virtual ServiceTurnaround ServiceTurnaround { get; set; }
    public virtual Project Project { get; set; }
    public virtual ICollection<FileLanguage> FileLanguages { get; set; }
    public virtual Language Language { get; set; }
    public virtual Language Language1 { get; set; }
}

@ah has reason, if lazy loading is enabled, the InjectFrom method should copy all the properties of the source File, even the navigation properties. This way, when you change the ToLanguage FK property value in the copy File, EF ignore that value and save the changes based in the Language navigation property you have already a set before. I think the order of the transactions is important here, first EF should update the ToLanguage FK row in the File Table related to the source File and after that, it should insert the copy File.

If you disable lazy loading, your navigation properties aren't loaded and they are not updated in the copy File, that's way when you set the ToLanguage FK property, the relationship is saved as you want.

If you want to work with lazy loading, I suggest you set in null that navigation property ( Language ) before save the changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM