3

EFはこの1つのテーブルを作成していないようです

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

1 に答える 1

1

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

そして、それは完璧に動作します!テーブルが作成され、問題なくエントリを追加できます。

于 2013-10-26T14:26:12.790 に答える