2016-06-09 5 views
0

나는 ID와 EF로 기본 MVC 프로젝트를 시작했다. 내 앱에서 사용자는 일부 레코드를 만들고 편집 할 수 있습니다. 이러한 레코드의 테이블에서 레코드를 만든 사용자 ID와 마지막으로 업데이트 한 사용자 ID를 갖고 싶습니다.ID 테이블을 사용하여 테이블을 조인하는 방법은 무엇입니까?

내 모델 클래스는 같은 것이다 : 나는 이러한 레코드를 나열하고 때

[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; 
      } 
    } 
} 

가, 나 또한 내 페이지를 원하는 :

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; } 
} 

그리고 RecordsController에서

, 나는 이런 식으로 dB로 새 레코드를 저장 이 사용자의 사용자 이름을 표시하십시오. 내가 만든 저장소에서 모든 활성 레코드를 가져옵니다. 내가 변경해야 할

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

:

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

그리고 이것은 저장소 코드는? 이런 용도로 샘플 코드를 줄 수 있습니까? 감사합니다.

+0

다음은 [예]입니다 (http://danieleagle.com/blog/2014/05/setting-up-asp-net-identity-fra). mework-2-0-with-database-first-vs2013-update-2-spa-template /). 희망 ~ ~ –

답변

0

당신은 같은 것으로

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

을 변경해야 새로운 RecordRepository를 작성하는 동안

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

설정 ID의의()

그런 Record.CreatingUser.FirstName로 액세스 ect

+0

Thnx. 레코드 모델 클래스에 탐색 속성을 추가하려고했지만 마이그레이션을 추가 할 때 오류가 발생했습니다. 하지만 귀하의 답변 후 나는 그것을 주장 :) 귀하의 기록 및 신원 DbSets 다른 DbContexts에 있기 때문에 마이 그 레이션을 만드는 오류가 발생했습니다. 그래서 기본 ApplicationDbContext를 제거하고 IdentityDbContext 에서 파생 된 다른 DbContext를 만들었습니다. 그러나 계속해서 다른 DbContext에서 두 개의 DbSets를 조인 할 것입니다. 나는 EntityFramework의 초보자이며 배워야한다. – HisMajesty

+0

단지 시간을 절약하기 위해 두 개의 다른 DbContext에서 DbSets에 참여할 수 없다. 동일한 컨텍스트에서 DbSets를 만듭니다. 다른 팁 - 레코드를 삽입 할 때 Object.Property 필드에 액세스하려면 컨텍스트/엔티티/탐색 속성을 새로 고쳐야합니다. 이 예제를 확인하십시오 : http://stackoverflow.com/questions/9081244/reload-an-entity-and-all-navigation-property-association-dbset-entity-framework –

관련 문제