エンティティ フレームワークと 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 でこれら 2 つのテーブルを結合しようとしています。
テスト済みの 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 に変換します。
ActionResult コード:
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?)
では、これら 2 つのエンティティを何らかの方法で接続して、お互いを認識できるようにする必要がありますか?