簡體   English   中英

在實體框架中使用來自不同實體的相同數據庫表-代碼優先

[英]Use same db table from different entities in Entity Framework- Code first

我前幾次使用實體框架數據庫。 現在,我正在嘗試先處理代碼。 在某些地方,我有點困惑。 可以說,我想擁有兩個類: ApplicationUserCurrentUser 所有這些類都打算使用相同的數據庫表Users Users表的結構看起來像-

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

這是課程的詳細信息-

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

這可能是一件容易的事,或者有一些棘手的技術可以做到這一點。 有什么幫助嗎?

您可以使用“ 每個層次結構的表”繼承策略:

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>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM