添加到多个表中查看模型

时间:2017-01-24 09:57:58

标签: c# asp.net-mvc entity-framework

我有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用户名,我需要ChatVM中包含from的id,name和lastname的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; }
}

2 个答案:

答案 0 :(得分:1)

所以你有一个" 1-1" UserDTO和ChatDTO实体之间的连接,每个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 ;

认为您的外键实际上是在表之间实现连接,因此通过它们可以完全访问主类。

答案 1 :(得分:0)

你应该使用

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


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