1

这里是实体框架和 mvc 的初学者。我有 2 个表模型:

用户资料

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
}

聊天记录

[Table("ChatLogs")]
public class ChatLogs
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ChatLogId { get; set; }
    [Column("Message")]
    public string Message { get; set; }
    [Column("UserId")]
    public int UserId { get; set; }

    public override string ToString()
    {
        return "Person: " + Message + " " + UserId;
    }

}

表 ChatLogs 中的 UserId 是 UserPorfile UserId 主键的外键。

我正在尝试在 Asp.NET MVC 4 中加入这两个表

测试的 SQL 查询:

select * from UserProfile as a join ChatLogs as b on a.UserId = b.UserId

测试过的 Linq 查询:

from b in db.ChatLogs
select new {
  ChatLogId = b.ChatLogId,
  Message = b.Message,
  UserId = b.UserId,
  Column1 = (int?)b.UserProfile.UserId,
  UserName = b.UserProfile.UserName,
  Email = b.UserProfile.Email
}

我正在使用名为“Linqer”的软件进行学习。它将 SQL 转换为 Linq。

动作结果代码:

private ChatLogContext db = new ChatLogContext();
public ActionResult Index()
{
    var list = from b in db.ChatLogs
               select new
               {
                   ChatLogId = b.ChatLogId,
                   Message = b.Message,
                   UserId = b.UserId,
                   Column1 = (int?)b.UserProfile.UserId,
                   UserName = b.UserProfile.UserName,
                   Email = b.UserProfile.Email
                   };


    var vm = new ChatLogsViewModel { LogListString = string.Join("\n", list) };
    return View(vm);

}

ChatLogViewModel 只有一个字符串变量,用于在视图中打印列表。

但我收到一个错误:

'Chat.Models.ChatLogs' does not contain a definition for 'UserProfile' and no extension method 'UserProfile' accepting a first argument of type 'Chat.Models.ChatLogs' could be found (are you missing a using directive or an assembly reference?)

那么我是否必须以某种方式连接这两个实体,以便他们相互了解?

4

2 回答 2

1

尝试这个:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    **public virtual ICollection<ChatLogs> ChatLogs { get; set; }**
}

[Table("ChatLogs")]
public class ChatLogs
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ChatLogId { get; set; }
    [Column("Message")]
    public string Message { get; set; }
    [Column("UserId")]
    public int UserId { get; set; }

    **public virtual UserProfile UserProfile { get;set; }**

    public override string ToString()
    {
        return "Person: " + Message + " " + UserId;
    }

}
于 2013-08-05T17:43:39.227 回答
0

最简单的连接方法是将聊天日志作为列表提供给用户:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }

    public List<ChatLog> ChatLogs{ get; set;}
}

现在您可以执行以下操作:

var users = Users.Include("ChatLogs");

现在,每个用户都将正确填写其 ChatLogs 列表。

于 2013-08-05T17:44:40.120 回答