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:
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)
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.