2014-04-06 3 views
0

엔티티 프레임 워크 6 (데이터베이스 모델에서 생성)을 사용하고 있으며 사용자라는 테이블이 있습니다.이 중 UserXml, UserExtranet, UserCustomer라는 관련 테이블이 있습니다. 이 테이블에는 동일한 필드가 있습니다.비슷한 속성을 가진 엔티티 프레임 워크 6 엔티티 확인

테이블은 외래 키로 연결됩니다.

사용자 유형 (예 : Xml/엑스트라 넷/고객)에 따라 UserXml을 사용하도록 설정하거나 UserExtranet을 사용하도록 설정해야합니다. 여기

나는 순간

//find the user 
User oUser = Context.User.FirstOrDefault(u => u.Email == this.Email); 

if (oUser != null) 
{ 
    var oUserType = (dynamic)null; 
    int iMaxAttempts; 
    bool bValidIp = false; 

    //authenticate the type of user 
    switch (this.Type) 
    { 
     case UserType.Xml: 
      oUserType = oUser.UserXml; 
      //do the validation   
      break; 

     case UserType.Api: 
      oUserType = oUser.UserApi; 
      //do the validation   
      break; 
     default: 
      oUserType = null; 
      break; 
    } 

}

여기

내 실체가에서 무슨의 조각입니다.

사용자 클래스

public partial class User 
{ 
    public User() 
    { 
    } 

    public long UserId { get; set; } 
    public string Email { get; set; } 

    public virtual UserCustomer UserCustomer { get; set; } 
    public virtual UserExtranet UserExtranet { get; set; } 
    public virtual UserXml UserXml { get; set; } 
} 

고객 클래스

public partial class UserCustomer 
{ 
    public UserCustomer() 
    { 

    } 

    public long UserId { get; set; } 
    public long SiteId { get; set; } 
    public string Email { get; set; } 
    public string Password { get; set; } 
    public string Salt { get; set; } 
    public byte FailedAttempts { get; set; } 
    public System.DateTime LastLogin { get; set; } 
    public bool Enabled { get; set; } 
    public bool Deleted { get; set; } 
    public System.DateTime CreatedOn { get; set; } 
    public System.DateTime LastChanged { get; set; } 

    public virtual User User { get; set; } 
} 

UserExtranet 클래스

public partial class UserExtranet 
{ 
    public UserExtranet() 
    { 
    } 

    public long UserId { get; set; } 
    public long SiteId { get; set; } 
    public string Password { get; set; } 
    public string Salt { get; set; } 
    public byte FailedAttempts { get; set; } 
    public bool Enabled { get; set; } 
    public bool Deleted { get; set; } 
    public System.DateTime CreatedOn { get; set; } 
    public System.DateTime LastChanged { get; set; } 

    public virtual User User { get; set; } 
} 

UserXml 클래스

public partial class UserXml 
{ 
    public UserXml() 
    { 
    } 

    public long UserId { get; set; } 
    public string Password { get; set; } 
    public string Salt { get; set; } 
    public byte FailedAttempts { get; set; } 
    public bool Enabled { get; set; } 
    public bool Deleted { get; set; } 
    public System.DateTime CreatedOn { get; set; } 
    public System.DateTime LastChanged { get; set; } 

    public virtual User User { get; set; } 
} 

각 테이블의 코드를 두 배로 늘리지 않고이를 수행하는 가장 좋은 방법은 무엇입니까?

답변

1

UserModel이라는 네 번째 클래스를 만들고 여기에 다양한 User 개체를 캐스팅 한 다음 UserModel의 유효성을 검사합니다.

public partial class UserModel 
{ 
public UserModel() 
{ 

} 

public long UserId { get; set; } 
public long SiteId { get; set; } 
public string Email { get; set; } 
public string Password { get; set; } 
public string Salt { get; set; } 
public byte FailedAttempts { get; set; } 
public System.DateTime LastLogin { get; set; } 
public bool Enabled { get; set; } 
public bool Deleted { get; set; } 
public System.DateTime CreatedOn { get; set; } 
public System.DateTime LastChanged { get; set; } 

public virtual User User { get; set; } 
} 

그리고

//find the user 
User oUser = Context.User.FirstOrDefault(u => u.Email == this.Email); 

if (oUser != null) 
{ 
var oUserType = (dynamic)null; 
int iMaxAttempts; 
bool bValidIp = false; 

var ModelUser = new UserModel(){ UserId = oUser,UserId, etc.}; 

//authenticate the type of user 
switch (this.Type) 
{ 
    case UserType.Xml: 
     oUserType = oUser.UserXml; 

     break; 

    case UserType.Api: 
     oUserType = oUser.UserApi; 

     break; 
    default: 
     oUserType = null; 
     break; 
} 
ModelUser.Validate(); 
} 
+0

감사합니다, 난 그냥 유사한 방식으로 어쩌면 AutoMapper을 사용할 수 있었다 다른 생각? – Tommassiov

+0

예. 필자는 AutoMapper에 대해 언급하고 싶었지만 매핑이 발생하는 것이 중요하다고 생각하지 않았기 때문에이를 반대했습니다. 하지만 확실히 사용자 AutoMapper. 전문가 팁 : AutoMapper를 EF와 함께 사용하는 경우 IEnumerable 대신 IQueryable을 반환하여 열거 형이 필요할 때만 열거되도록합니다. 메모리 관리 및 CPU 시간이 훨씬 좋습니다. –

+0

Entity Framework 및 이와 유사한 엔터티를 사용할 때 이것이 일반적인 방법입니까? – Tommassiov

관련 문제