2

我是新手mvc。我已经创建了一个MVC应用程序,我在其中使用了code first方法。现在我有两张表DealComment。现在我想在数据库中添加一个新表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

2

尝试在包管理器控制台中使用“update-database -force -verbose”。

如果它不起作用,请修改迁移但键入“add-migration somename”,它将出现在迁移文件夹中。

如果您不熟悉 MVC 和 EF,请务必查看本教程。它解释了所有相关内容以及您需要了解的所有其他内容:

http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m1-intro&mode=live&clip=0&course=mvc4-building

于 2013-08-19T10:27:40.783 回答
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 回答