0

I started a default MVC project with Identity and EF. In my app users will be able to create and edit some records. In the table for these records, I want to have the ids of users who created the record and who updated lastly.

My model class is like:

public class Record
{
    public int ID { get; set; }
    public DateTime CreateTime { get; set; }
    public string CreatingUserID { get; set; }
    public string UpdatingUserID { get; set; }
    public DateTime UpdateTime { get; set; }
    public Enums.RecordStatus Status { get; set; }
}

And in RecordsController, I save new records to db like this:

    [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(FormCollection form, RecordCreateVM vm)
    {

        string userId = User.Identity.GetUserId();
        DateTime now = DateTime.Now;
        Record rec = new Record ();

        if (ModelState.IsValid)
        {
            int newRecordId;

            using (RecordRepository wr = new RecordRepository())
            {
                UpdateModel(rec);
                rec.CreateTime = now;
                rec.UpdateTime = now;
                rec.CreatingUserID = userId;
                rec.UpdatingUserID = userId;
                rec.Status = Enums.RecordStatus.Active;

                Record result = wr.Add(rec);
                wr.SaveChanges();
                newRecordId = result.ID;
            }
    }
}

When I am listing these records, I also want my page to display these users' usernames. I get all the active records from the repository I created.

    public ActionResult Index()
    {
        RecordListVMviewModel = new RecordListVM();
        using (RecordRepository wr = new (RecordRepository())
        {
            viewModel.Records = wr.GetAll();
        }
        return View(viewModel);
    }

And this is the repository code:

public class RecordRepository: Repository<Record>
{
    public override List<Record> GetAll()
    {
        IQueryable<Record> activeRecords = DbSet.Where(w => w.Status == Enums.RecordStatus.Active);
        return activeRecords.ToList();
    }      
}

Where do I have to make changes? Can you give me an sample code for usages like this? Thank you.

  • Here's an [example](http://danieleagle.com/blog/2014/05/setting-up-asp-net-identity-framework-2-0-with-database-first-vs2013-update-2-spa-template/). Hope it helps~~ – Shawn Yan Jun 10 '16 at 02:24

1 Answers1

0

You need to change

public string CreatingUserID { get; set; }
public string UpdatingUserID { get; set; }

to something like:

public User CreatingUser { get; set; }
public User UpdatingUser { get; set; }

Set the ID's during the creation of new RecordRepository()

Then access them as Record.CreatingUser.FirstName ect

Steve Stokes
  • 1,200
  • 18
  • 36
  • Thnx. I also tried adding navigation properties to Record model class, but I got errors when adding migration. But after your answer I insisted on it :) Errors in creating migrations occured because my Record and Identity DbSets are in different DbContexts. So I removed default ApplicationDbContext, and made my other DbContext derive from IdentityDbContext. But I will continue to search joining two DbSets from different DbContexts. I am a newbie for EntityFramework and I should learn :) – serhankars Jun 11 '16 at 05:47
  • Just to save you time, you cannot join DbSets from two different DbContexts. Create the DbSets from the same context. Another tip - when you insert a record you will need to refresh the context/entities/navigation properties in order to access the Object.Property fields. Check out this example: http://stackoverflow.com/questions/9081244/reload-an-entity-and-all-navigation-property-association-dbset-entity-framework – Steve Stokes Jun 14 '16 at 20:53