Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无效的对象名称';dbo.UserNotes';。异常-ASP.NET MVC 4_C#_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

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

C# 无效的对象名称';dbo.UserNotes';。异常-ASP.NET MVC 4,c#,asp.net,asp.net-mvc,entity-framework,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.Securi

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; }
}
}
我尝试过的事情:

  • 将模式更改为“dbo”(通过添加[Table(“UserNotes”,模式:“dbo”)]))
  • 从Visual Studio手动删除整个数据库文件
  • 将plurar形式更改为单数形式和背面形式
  • 重新安装实体框架
有什么想法吗?也许我在做一些愚蠢的事情却没有注意到

编辑: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)
public类用户上下文:DbContext
{
公共用户上下文()
:base(“默认连接”)
{
}
公共数据库集用户配置文件{get;set;}
}
[表(“用户档案”)]
公共类用户配置文件
{
[关键]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
}
(...)
(其他型号)

在阅读了格特·阿诺德的评论后,我这样做了

已将此行添加到UsersContext:

public DbSet<UserNotes> Notes { get; set; }
public DbSet Notes{get;set;}
这就是它现在的样子:

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

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Notes> Notes { get; set; }
}
public类用户上下文:DbContext
{
公共用户上下文()
:base(“默认连接”)
{
}
公共数据库集用户配置文件{get;set;}
公共DbSet注释{get;set;}
}

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

如果您这样做:
public DbSet Notes{get;set;}
并将其更改为:
public DbSet UserNotes{get;set;}
,该怎么办。我认为它必须符合命名约定?表是否存在于数据库中?什么类型的类是
UserProfile
?我的意思是,它是否与其他两个类一样是映射类模型的一部分?必须有一个包含所有类的上下文才能生成数据库。见此:
public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

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