c# - 对象名称 'dbo.UserNotes' 无效。异常 - ASP.NET MVC 4

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

EF 似乎没有创建这个表

NotesModels.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Security;

namespace note.DO.Models
{
public class NotesContext : DbContext
{
    public NotesContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserNotes> Notes { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
}

[Table("UserNotes")]
public class UserNotes
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int NoteId { get; set; }

    public virtual int UserId { get; set; }

    [ForeignKey("UserId")]
    public virtual UserProfile User { get; set; }

    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime? Added { get; set; }
    public DateTime? Edited { get; set; }  
}

public class CreateNoteModel
{
    [Display(Name = "Author")]
    public UserProfile Author { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Title")]
    public string Title { get; set; }

    [Required]
    [DataType(DataType.MultilineText)]
    [Display(Name = "Content")]
    public string Content { get; set; }

    [DataType(DataType.DateTime)]
    [Display(Name = "Added on")]
    public DateTime? AddedOn { get; set; }
}
}

我使用这些类生成了 Controller 。但是,当我尝试添加注释时,出现以下异常:

Invalid object name 'dbo.UserNotes'.

...由这一行触发:

db.SaveChanges();

我尝试过的事情:

  • 将架构更改为“dbo”(通过添加 [Table("UserNotes", Schema:"dbo")]))
  • 从 Visual Studio 中手动删除整个数据库文件
  • 将复数形式更改为单数形式并返回
  • 重新安装 Entity Framework

有什么想法吗?也许我正在做一些非常愚蠢的事情而没有注意到?

编辑:UserProfile 类和数据上下文:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

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

(...)

(other models)

最佳答案

阅读 Gert Arnold 的评论后,我这样做了

将此行添加到 UsersContext:

public DbSet<UserNotes> Notes { get; set; }

现在是这样的:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Notes> Notes { get; set; }
}

而且它完美运行!现在正在创建表,我可以毫无问题地添加条目。

关于c# - 对象名称 'dbo.UserNotes' 无效。异常 - ASP.NET MVC 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19565142/

相关文章:

来自多个线程的 C# 数据表添加方法?

c# - StreamReader 问题

ASP.NET TextBox TextMode 设置为 Date 在 IE 11 中不起作用

html - 如何在 ASP.NET MVC 中获取模型状态错误的集合?

c# - XamlParseException 是未处理的 C# 应用程序

c# - C#数据库查询错误消息?

c# - 无法将字符串隐式转换为文本框

c# - ASP.NET 保存复杂的实体模型

jquery - 获取JQGrid格式化程序: ‘showlink’ to work in MVC

mysql - 在 MVC 中添加列以连接表和有效负载