기본 ID 인증을 사용하는 MVC 5 응용 프로그램이 있습니다. 사용자 프로필은 우리 모델의 일부입니다. 즉, UserInfo에 외래 키가있는 클래스가 여러 개 있습니다. 두 개의 DbContext가 있으며 하나는 모델 용이고 다른 하나는 UserInfo 용입니다.MVC5 Identity + EntityFramework의 다중 컨텍스트
public class ApplicationDbContext : IdentityDbContext<UserInfo>
{
public ApplicationDbContext()
: base("Profile")
{
}
}
public class UserInfo : IdentityUser
{
public UserInfo()
{
LibraryItems = new List<LibraryItem>();
if (UserGroup == UserGroupEnum.ANALYST)
{
Companies = new List<Company>();
}
DateCreate = DateTime.Now;
DateUpdate = DateTime.Now;
DateDelete = DateTime.Now;
}
[DisplayColumnInIndex]
[DisplayName("Is Active ?")]
public bool IsActive { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
//[ValidateFile(ErrorMessage = "Please select a PNG image smaller than 1MB")]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[Editable(false, AllowInitialValue = true)]
public DateTime DateCreate { get; set; }
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateUpdate { get; set; }
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateDelete { get; set; }
[DisplayColumnInIndex]
public String Name { get; set; }
[DisplayColumnInIndex]
public String FirstName { get; set; }
[DisplayColumnInIndex]
public String Pseudo { get; set; }
[DisplayColumnInIndex]
public string PhoneNumber { get; set; }
[DisplayColumnInIndex]
public String Company { get; set; }
[DisplayName("Name_Id")]
[ForeignKey("FullName")]
public int? NameId { get; set; }
[DisplayColumnInIndex]
public UserGroupEnum UserGroup { get; set; }
[DisplayColumnInIndex]
public UserStatusEnum UserStatus { get; set; }
public virtual Name FullName { get; set; }
}
public class Comment
{
// more properties
[DisplayName("UserInfoId")]
[ForeignKey("UserInfo")]
public virtual String UserInfoId { get; set; }
}
public class Like
{
// more properties
[DisplayName("UserInfoId")]
[ForeignKey("UserInfo")]
public virtual String UserInfoId { get; set; }
}
내 userInfo에는 두 번째 DBontext의 다른 테이블에 대한 외래 키가 있습니다.
인증 시스템을 설계하는 가장 좋은 방법은 무엇입니까? 두 문맥 사이의 관계를 유지해야한다.
미리 감사드립니다.
실제로. Identity 사용자 엔티티와 다른 엔티티 사이에 관계가있는 경우 모두 동일한 컨텍스트에 있어야합니다. 그렇지 않으면 하나의 컨텍스트에서 사용자를 얻은 다음 여러 가지 쿼리를 수행해야합니다. 그런 다음 외래 키 속성의 값을 사용하여 다른 컨텍스트에서 관련 개체를 찾습니다. 실행할 수는 있지만 일을 처리하는 가장 효율적인 방법은 아닙니다. –
글쎄, 아니,하지만 다른 이유로. 사용자 엔티티에 별도의 컨텍스트에서 원하는 다른 유형의 탐색 특성이있는 경우, 사용자 유형은 실제로 사용자 클래스가 상주하는 컨텍스트의 일부입니다. EF가 모든 엔티티를 소유하지 않는 한 관계를 관리 할 수 없으므로 엔티티가 관련되어 있다면 기본적으로 자체 DbSet없이 참조 된 엔티티에 대해 암시적인'DbSet'을 생성합니다. 즉, 해당 컨텍스트의 마이그레이션은 해당 관련 엔터티에 대한 테이블을 만들 것이며 다른 컨텍스트에서 동시에 관리 할 수는 없습니다. –
탐색 속성이 없으면 지연로드가 없으므로 실제로 작업해야하는 것은 일부 값 유형 "외래 키"속성입니다. –