locked
Some properties in entity not being added during insert RRS feed

  • Question

  • I have a somewhat complex entity that I am trying to save into my db. The odd thing about it is that although I am setting a few of the properties before doing the Add call, those properties are not being added to the db. The properties are CreatedBy and LastModifiedBy. I've run the sql profiler and checked the actual insert statement and those entities do not come over into the insert statement. The entity has a inheritance and partial class structure. Its indicated below in the code sample.

    public partial class JobRequest
        {
            public JobRequest()
            {
                this.JobOutputs = new HashSet<JobOutput>();
                this.JobRequestStatusSteps = new HashSet<JobRequestStatusStep>();
            }
        
            public int JobRequestId { get; set; }
            public int JobDefintionId { get; set; }
            public int JobPriorityId { get; set; }
            public string JobStatus { get; set; }
            public string RequestedBy { get; set; }
            public System.DateTime RequestedOn { get; set; }
            public int ProcessID { get; set; }
            public string MachineName { get; set; }
            public string Parameters { get; set; }
            public string OutputMessage { get; set; }
            public string CreatedBy { get; set; }
            public System.DateTime CreatedOn { get; set; }
            public string LastModifiedBy { get; set; }
            public Nullable<System.DateTime> LastModifiedOn { get; set; }
            public Nullable<System.DateTime> StartTime { get; set; }
            public Nullable<System.DateTime> EndTime { get; set; }
            public string TechnicalOutput { get; set; }
        
            public virtual JobDefinition JobDefinition { get; set; }
            public virtual ICollection<JobOutput> JobOutputs { get; set; }
            public virtual JobPriority JobPriority { get; set; }
            public virtual ICollection<JobRequestStatusStep> JobRequestStatusSteps { get; set; }
        }

    public partial class JobRequest : K2.Common.Interfaces.IAuditable
        {
        }

    public interface IAuditable
        {
            string CreatedBy { get; set; }
            DateTime CreatedOn { get; set; }
            string LastModifiedBy { get; set; }
            Nullable<System.DateTime> LastModifiedOn { get; set; }
        }

    public JobRequest InsertNewJob(string pJobName, string pParametersXml, string pUserName)
            {
                using (var context = new AsyncJobEntities())
                {
                    var lJobDefinition = context.JobDefinitions.Where(l => l.JobName == pJobName).FirstOrDefault();
                    var lJobRequest = new JobRequest
                    {
                        JobDefintionId = lJobDefinition.JobDefinitionId,
                        JobPriorityId = lJobDefinition.JobPriorityID,
                        JobStatus = JobStatus.ACCEPTED.ToString(),
                        RequestedBy = pUserName,
                        RequestedOn = DateTime.Now,
                        ProcessID = 0,
                        MachineName = "",
                        Parameters = pParametersXml,
                        OutputMessage = "",
                        StartTime = DateTime.Now
                    };
    
                    SetAuditColumnsOnCreate(lJobRequest, pUserName);
                    context.JobRequests.Add(lJobRequest);
                    context.SaveChanges(pUserName);
    
                    return lJobRequest;
                }
            }

    public int SaveChanges(string pUserName)
            {
                // Handle audit columns for added items
                foreach (var entry in ChangeTracker.Entries().Where(p => p.State == System.Data.EntityState.Added && p.Entity is IAuditable))
                {
                    IAuditable e = (IAuditable)entry.Entity;
                    e.CreatedBy = pUserName;
                    e.CreatedOn = DateTime.Now;
                }
    
                // Handle audit columns for updated items
                foreach (var entry in ChangeTracker.Entries().Where(p => p.State == System.Data.EntityState.Modified && p.Entity is IAuditable))
                {
                    IAuditable e = (IAuditable)entry.Entity;
                    e.LastModifiedBy = pUserName;
                    e.LastModifiedOn = DateTime.Now;
                }
    
                // Let the DbContext persist changes back to the db
                return base.SaveChanges();
            }


    • Edited by dharric72 Friday, March 2, 2012 9:24 PM
    Friday, March 2, 2012 9:22 PM

Answers

  • Turns out that we had done a migration of the edm from another db and during the migration somehow the StoreGenerated pattern got set to computed. That's why we could not get it to reset to anything else. Don't know why the migration would do that, but switching it to none fixed the issue.
    • Marked as answer by dharric72 Thursday, March 15, 2012 12:38 AM
    Thursday, March 15, 2012 12:38 AM

All replies

  • Hi dharric,

    Welcome to MSDN Forum.

    I couldn't find anything wrong with the code, could you please upload the project to SkyDrive and post the link here?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 2:41 AM
  • Hi dharric72,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 8, 2012 2:05 AM
  • Interesting.


    JP Cowboy Coders Unite!

    Thursday, March 8, 2012 2:12 AM
  • Turns out that we had done a migration of the edm from another db and during the migration somehow the StoreGenerated pattern got set to computed. That's why we could not get it to reset to anything else. Don't know why the migration would do that, but switching it to none fixed the issue.
    • Marked as answer by dharric72 Thursday, March 15, 2012 12:38 AM
    Thursday, March 15, 2012 12:38 AM
  • Have you used Entity Framework Code First migrations?

    thanks,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, March 15, 2012 1:19 AM