Invalid object name 'dbo.UserNotes'. exception - ASP.NET MVC 4

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

Question

It appears that EF doesn't create this one table

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; }
}
}

I generated controller using those classes. However, when I attempt to add a note, I get following exception:

Invalid object name 'dbo.UserNotes'.

...triggered by this line:

db.SaveChanges();

Things I have tried:

  • changing schema to "dbo" (by adding [Table("UserNotes", Schema:"dbo")]))
  • manually removing entire database file from Visual Studio
  • changing plurar forms to singular and back
  • reinstalling Entity Framework

Any ideas? Perhaps there is something really stupid that I'm doing and not noticing?

EDIT: UserProfile class and data context:

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)
1
3
10/25/2013 9:58:12 AM

Accepted Answer

After reading Gert Arnold's comment, I did this

Added this line to UsersContext:

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

So this is how it looks now:

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

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

And it works flawlessly! Table is now being created and I can add entries without any problems.

1
10/26/2013 2:26:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow