如何用我自己的上下文连接Asp.Nets数据库?

时间:2019-01-03 12:57:04

标签: c# .net database dbcontext

我正在尝试使用单个用户帐户在Mvc中构建应用程序,并且试图将自动生成的数据库与自己的上下文相连接。 尝试创建Create方法的视图时遇到的问题是出现此错误消息:

  

运行所选代码生成器时发生错误:“无法执行   检索“ Projekt_Dejtingsida.Models.Messages”的元数据不是   有效。在导航栏上找不到导航属性“ ApplicationUser”   依赖类型'Projekt_Dejtsida.Models.Messages'。名称值   应该是有效的导航属性名称。

请帮助新手!

这是我正在使用的模型:

我已经尝试过protected override void OnModelCreating(DbModelBuilder modelBuilder)方法,但是没有用...

public class Messages {
    [Key, ForeignKey("ApplicationUser")]
    public string Id { get; set; }
    [Key]
    public int MessageId { get; set; }
    public virtual ApplicationUser Sender { get; set; }
    public virtual ApplicationUser Receiver { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "You can't send a message without a content")]
    [StringLength(300, MinimumLength = 3, ErrorMessage = "Your message should be between 3 and 300 characters")]
    public string MessageContent { get; set; }
} 

public class FriendRequests {
    public string UserId { get; set; }
    [Key]
    public int RequestId { get; set; }
    public virtual ApplicationUser RequestTo { get; set; }
    public virtual ApplicationUser RequestFrom { get; set; }
    public bool Confirmed { get; set; }
}

public class Profile {
    [Key]
    public string UserId { get; set; }
    public byte[] ProfilePicture { get; set; }
    public string Firstname { get; set; }
    public int Age { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
    public string About { get; set; }
    public virtual ICollection<Messages> MyMessages { get; set; }

}

这是DbContext:

public class DatesiteContext : DbContext {

    public DatesiteContext() : base() { }       

    public DbSet<FriendRequests> Requests { get; set; }
    public DbSet<Messages> Messages { get; set; }
    public DbSet<Profile> Profiles { get; set; }
}

这是MessageController

[Authorize]
public class MessageController : Controller
{
    // GET: Message
    public ActionResult Index() 
    {
        var db = new DatesiteContext();
        var userId = User.Identity.GetUserId();
        var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

        return View(new MessageViewModel {
            Sender = msgs.Sender,
            Receiver = msgs.Receiver,
            MessageContent = msgs.MessageContent
        });
    }

    public ActionResult Create(MessageViewModel model) {
        DatesiteContext db = new DatesiteContext();

        var userId = User.Identity.GetUserId();
        var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

        if (msgs == null) {
            db.Messages.Add(new Messages {
                Id = userId,
                Sender = msgs.Sender,
                Receiver = msgs.Receiver,
                MessageContent = msgs.MessageContent
            });
        } else {
            msgs.Sender = model.Sender;
            msgs.Receiver = model.Receiver;
            msgs.MessageContent = model.MessageContent;
        }

        db.SaveChanges();

        return RedirectToAction("Index", "Profile");

    }
}

0 个答案:

没有答案