无效的对象名称“dbo”.用户注释'.例外 - ASP.NET MVC 4
本文关键字:例外 ASP MVC NET 注释 对象 用户 dbo 无效 | 更新日期: 2023-09-27 18:36:39
EF 似乎没有创建这个表
备注型号.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"。用户注释'。
。由此行触发:
db.SaveChanges();
我尝试过的事情:
- 将架构更改为"dbo"(通过添加 [Table("UserNotes", Schema:"dbo")]))
- 手动从Visual Studio中删除整个数据库文件
- 将多体形式更改为单数并返回
- 重新安装实体框架
有什么想法吗?也许我正在做一些非常愚蠢的事情而没有注意到?
编辑:用户配置文件类和数据上下文:
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; }
}
而且它完美无缺!现在正在创建表,我可以毫无问题地添加条目。