0

I have a silverlight mvvm with ria project. I have a UI in which admin users can enter info to create new work orders. However, I am having trouble calling the db and adding a new record to the table. I have no code-behind for the UI, the controls are tied to the model through Commands and Command Parameters. So when a user clicks, 'Add new job' it comes here,

public class EditJobViewModel : ViewModelBase
{
    private Job _job;

    public Job CurrentJob
    {
        get { return _job; }
        set
        {
            _job = value;
            OnPropertyChanged("CurrentJob");
        }
    }

    public ICommand NewJob
    {
        get
        {
            return new DelegateCommand(BeginNewJob, (o) => true);
        }

    }


    public void BeginNewJob(object o)
    {

            _job = new Job();
            //_job.JobNumber = _job.JobID.ToString();
            _job.AssignedTo = App.userID;
            _job.AddedBy = App.userID;
            _job.FileTypeJob = "PDF";
            _job.AddedTS = DateTime.Now;
            _job.OpenDate = DateTime.Now;
            BeginSave(o);
        }
    }

Where Im having trouble is creating a new record in the 'Job' table. On my breakpoint it returns all the columns it needs to, just not a new 'JobID' which is my primary key. This is how I was supposedly trying to create a new record.

public void BeginSave(object o)
    {
        if (!IsDesignTime)
        {
            try
            {
                if (CurrentJob.EntityState == EntityState.New)
                {
                    CurrentJob.AddedBy = App.userID;
                    CurrentJob.AddedTS = DateTime.Now;
                }
                CurrentJob.UpdatedBy = App.userID;
                CurrentJob.UpdatedTS = DateTime.Now;

                // This is here because of a bug in infragistics grid/Entity Framework 
                foreach (JobFileType ft in CurrentJob.JobFileTypes)
                {
                    if (ft.EntityState != EntityState.Unmodified)
                        (ft as IEditableObject).EndEdit();
                }

                foreach (JobTag tag in CurrentJob.JobTags)
                {
                    if (tag.EntityState != EntityState.Unmodified)
                        (tag as IEditableObject).EndEdit();
                }
                //(CurrentJob as IEditableObject).EndEdit();

                SubmitOperation s = _context.SubmitChanges();
                if (s.HasError)
                { }
            }
            catch (Exception ex)
            { }
        }
    }

Except that it never hits the EntityState.new. That's just the way I thought to try it. Im thinking there a way to do it from the 'BeginNewJob' command but unable to find a way to create a new JobID or record in general. The Database already has 10000 records and has multiple users creating jobs, so I need a way to get the last job created (getMaxID()??) and increment appropriately, creating a new job on the spot.

1 Answer 1

1

İf you use guid type for id column, you will not need to find next id and this approach will decouple new objects from previous objects.

Sign up to request clarification or add additional context in comments.

1 Comment

ok, ill give that a try, implement to the model or someplace else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.