2017-01-24 35 views
0

我有2個表格,tblUserstblChat加入查看多個表格中的模型

向tblUsers

[Id]   INT   IDENTITY (1, 1) NOT NULL, 
[FirstName] NVARCHAR (50) NULL, 
[LastName]  NVARCHAR (50) NULL, 
[EmailAddress] NVARCHAR (100) NULL, 
[Username]  NVARCHAR (50) NULL, 
[Password]  NVARCHAR (50) NULL, 
PRIMARY KEY CLUSTERED ([Id] ASC) 

tblChat

[Id]  INT   IDENTITY (1, 1) NOT NULL, 
[From]  NVARCHAR (50) NULL, 
[To]  NVARCHAR (50) NULL, 
[Message] NVARCHAR (MAX) NULL, 
[DateSent] DATETIME2 (7) NULL, 
[Read]  BIT   NULL, 
PRIMARY KEY CLUSTERED ([Id] ASC) 

我也有各自的DTO:

UserDTO

[Table("tblUsers")] 
public class UserDTO 
{ 
    [Key] 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string EmailAddress { get; set; } 
    public string Username { get; set; } 
    public string Password { get; set; } 
} 

ChatDTO

[Table("tblChat")] 
public class ChatDTO 
{ 
    [Key] 
    public int Id { get; set; } 
    public string From { get; set; } 
    public string To { get; set; } 
    public string Message { get; set; } 
    public DateTime DateSent { get; set; } 
    public bool Read { get; set; } 

    [ForeignKey("From")] 
    public virtual UserDTO FromUsers { get; set; } 
    [ForeignKey("To")] 
    public virtual UserDTO ToUsers { get; set; } 
} 

我也有一個chat view model,我用中檢索所有從tblChat表肚裏像這樣的行:

public class ChatVM 
{ 
    public ChatVM() 
    { 
    } 

    public ChatVM(ChatDTO row) 
    { 
     Id = row.Id; 
     From = row.From; 
     To = row.To; 
     Message = row.Message; 
     DateSent = row.DateSent; 
     Read = row.Read; 
    } 

    public int Id { get; set; } 
    public string From { get; set; } 
    public string To { get; set; } 
    public string Message { get; set; } 
    public DateTime DateSent { get; set; } 
    public bool Read { get; set; } 
} 

的問題是,ChatVM是不夠的,我需要有更多的屬性基於users表。

from酒店距離users表uniqye用戶名,我需要在包含from的身份證,名字和姓氏的ChatVM 3個屬性。

基本上類似如下(被修改ChatVM

public class ChatVM 
{ 
    public ChatVM() 
    { 
    } 

    public ChatVM(ChatDTO row) 
    { 
     Id = row.Id; 
     From = row.From; 
     To = row.To; 
     Message = row.Message; 
     DateSent = row.DateSent; 
     Read = row.Read; 
     FromId = ???; 
     FromFirstName = ???; 
     FromLastName = ???; 
    } 

    public int Id { get; set; } 
    public string From { get; set; } 
    public string To { get; set; } 
    public string Message { get; set; } 
    public DateTime DateSent { get; set; } 
    public bool Read { get; set; } 

    public int FromId { get; set; } 
    public string FromFirstName { get; set; } 
    public string FromLastName { get; set; } 
} 

回答

1

所以,你必須UserDTO和ChatDTO實體之間的 「1-1」 連接,每個ChatDTO對象通過外鍵FromUsers和ToUsers具有2個UserDTO對象的引用。您的ChatVM構造函數接受一個ChatDTO對象作爲輸入,那麼如果您使用外鍵屬性來訪問UserDTO類的其他屬性呢?

e.g

 FromId = row.FromUsers.Id; //Also you can check if FromUsers property is null to avoid exceptions 
     FromFirstName = row.FromUsers.FirstName ; 
     FromLastName = row.FromUsers.LastName ; 

認爲,其實你的外鍵實現你的表之間的連接,所以通過它們你可以完全訪問主類。

0

你應該使用像

public class alldata 
{ 
public IEnumerable<tbl_chat> chatdata { get; set; } 


public IEnumerable<tbl_user> chatdata { get; set; } 
} 
+0

這與這個問題有什麼關係? –