2016-12-19 3 views
0

私はEntity Frameworkデータベースを最初から数回使用しています。さて、私は最初にコードにアプローチしようとしています。私は少し混乱しています。私は2つのクラスを持っていたいと言います。ApplicationUser & CurrentUserこれらのクラスはすべて、同じデータベーステーブルユーザを使用することを意図していました。ユーザーテーブルの構造は、ここでEntity Frameworkの異なるエンティティの同じdbテーブルを使用します。コード

----------------------------------------------------------------------------------------------------------- 
UserId | FullName | Designation | LoginName | LoginPassword | CreatedDateTime | IsActive | FewOtherFields | 
----------------------------------------------------------------------------------------------------------- 

like-に見えるのクラスは

//Will be used to add,edit, delete records for new user. 
ApplicationUser 
{ 
    UserId 
    FullName 
    Designation 
    LoginName 
    LoginPassword 
    CreatedDateTime 
    IsActive 
    ... 
    OtherProperties 
    ... 
} 

//Will be used to login and other purposes. 
CurrentUser 
{ 
    UserId 
    FullName 
    Designation 
    LoginName 
    LoginPassword 
    IsActive 
} 

を詳細 - され、これは簡単なことである、またはこれを達成するためにトリッキーなテクニックがあることがございます。どんな助け?

+0

あるCurrentUserがApplicationUserです。 CurrentUserクラスの背後にある理由は何ですか?いくつかのプロパティを隠すつもりですか? – JuanR

+0

なぜ2つの異なるクラスが同じものを表現したいのですか? 'CurrentUser'を使わなければならない場合は、' Application.server'を '.Select()'で投影してください。 – DavidG

+0

@ Juan-プロパティの隠蔽が理由です。他にもいくつかの問題があります。 –

答えて

0

あなたは階層継承戦略あたり表を使用することができます。

public class CurrentUser 
{ 
    //some properties 
} 

public class ApplicationUser : CurrentUser 
{ 
    //additional properties 
} 

public class MyContext : DbContext 
{ 
    public DbSet<CurrentUser> Users { get; set; } 
} 

使用法:

IQuerable<CurrentUser> currentUsers = context.Users; 
IQuerable<ApplicationUser> applicationUsers = context.Users.OfType<ApplicationUser>(); 
関連する問題