多个外键 [英] Multiple foreign key

查看:115
本文介绍了多个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有这样的模型类,我想使用FromUserId和ToUserId作为UserProfile表的外键。

Hi I have model class like this, I want to use FromUserId and ToUserId as foreign keys for UserProfile table.

public class Chat
    {
        [Key]
        public int MessageId { get; set; }
        [Required]
        public int FromUserId { get; set; }
        [Required]
        public int ToUserId { get; set; }
        [Required]
        public string Message { get; set; }
        [Required]
        public DateTime Time { get; set; }
        
        public virtual ICollection<UserProfile> UserProfiles { get; set; }
    }





甚至可能吗?如果不是你能看到这个吗?我在



Is that even possible ? If it's not could you look at this? I got unspecified error at

var name = db.UserProfiles.Find(2);

中收到了未指明的错误。我认为这是因为我尝试访问已经访问过的数据库。

. I think it's caused because I try to access a DB while it's already accessed.

public void getMessages()
       {
           var db = new UsersContext();
           var messages = from a in db.Messages where a.FromUserId== WebSecurity.CurrentUserId || a.ToUserId==WebSecurity.CurrentUserId select a;

           foreach(var a in messages)
           {
               if (a.FromUserId == WebSecurity.CurrentUserId)
               {
                   Clients.Caller.addNewMessageToPage(WebSecurity.CurrentUserName, a.Message);
               }
               else
               {
                   var name = db.UserProfiles.Find(2);
                   Clients.Caller.addNewMessageToPage(name.UserName, a.Message);
               }
           }
           db.Dispose();

       }

推荐答案

有人说,当你搜索时,你应该检查你找到了在使用之前的事情。您可以拥有任意数量的外键,这不是您的问题。您收到的任何错误,请将它们完全复制到您的问题中。还要告诉我们爆炸的确切线以及它使用的任何变量的状态。
As someone said, when you search, you should check you found something before working with it. You can have as many foreign keys as you like, that's not your issue. Any errors you get, copy them exactly in to your question. Also tell us the exact line that blows up and the state of any variables it uses.


这篇关于多个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆