2

私は初めてですmvc。アプローチを使用したMVCアプリを作成しましたcode first現在、 DealCommentの 2 つのテーブルがあります。ここで、データベースに新しいテーブルCategoryを追加し、Dealテーブルに新しい列categoryIdを追加したいと考えています。

データベースとモデルを更新するにはどうすればよいですか?

データベースに使用Sql Server 2008 R2しています。

私はクラスの構造に従っています:

      namespace FBWebApp.Models
    {
        public class Deal
      {
        public int ID { get; set; }                 // ID
        public string Title { get; set; }           // Titolo del deal   
        public string Description { get; set; }     // Descrizione dell'annuncio
        public string FacebookUID { get; set; }     // UID facebook dell'utente
        public string Visibility { get; set; }      // Visibility
        public string Category { get; set; }
        public int Option1 { get; set; }
        public int Option2 { get; set; }
        public int Option3 { get; set; }
        public int Option4 { get; set; }
        public string PhotoURL { get; set; }    // URL of the facebook photo profile 
        public string Name { get; set; }        // Name of the user
        public string ProfileUrl { get; set; }  // URL of the facebook profile
        public string Photo1 { get; set; }  // URL of the Photo1 (local )
        public string Photo2 { get; set; }
        public string Photo3 { get; set; }
        public string Photo4 { get; set; }
        public string Photo5 { get; set; }
    }
    public class Comment
    {
        [Key]
        public int CommentId { get; set; }
        public string CommentText { get; set; }
        public int ID { get; set; }
        [ForeignKey("ID")]
        public Deal DelNav { get; set; }
    }

    public class DealDBContext : DbContext
    {

        public DealDBContext() : base("DealDBContext") { }
        public DbSet<Deal> Deals { get; set; }
        public DbSet<Comment> Comments { get; set; }

    }
}
4

2 に答える 2

1

最初にモデルを追加します:

public class Category
{
  public int ID { get; set; }
  public int cateName { get; set; }
}

Dealクラスで:

public class Deal
    {
        //..
         [ForeignKey("CatId")]
         public virtual Category Category { get; set; }
    }

移行を有効にした後、コンソール マネージャーで次のコマンドを使用してデータベースを更新する必要があります。

update-database
于 2013-08-19T08:10:22.677 に答える