我是新来的 MVC 。我creted一个 MVC 应用程序,其中我已经使用 code首先办法。现在我有两个表的新政和注释。现在我想添加一个新表的类别在数据库中,新列的的categoryId 在交易表。
我怎么可以更新数据库和模型?
我用 SQL Server 2008的R2 数据库。
我下面的类结构:
命名空间FBWebApp.Models { 公共类交易 { 公众诠释ID {获得;组; } // ID 公共字符串名称{获取;组; } // TITOLO德尔协议 公共字符串描述{获得;组; } // Descrizione dell'annuncio 公共字符串FacebookUID {获得;组; } // UID Facebook的dell'utente 公共字符串能见度{获得;组; } // 能见度 公共字符串等级{获得;组; } 公众诠释选项1 {获得;组; } 公众诠释选项2 {获得;组; } 公众诠释选项3 {获得;组; } 公众诠释3选项{获得;组; } 公共字符串PhotoURL {获得;组; } // Facebook的照片资料页的网址 公共字符串名称{;组;用户} //名称 公共字符串ProfileUrl {获得;组; } //在Facebook的个人主页的网址 公共字符串照片1 {获得;组;在照片1的} // URL(本地) 公共字符串照片2 {获得;组; } 公共字符串照片3 {获得;组; } 公共字符串照片4 {获得;组; } 公共字符串照片5 {获得;组; } } 公共类评论 { [键] 公众诠释CommentId {获得;组; } 公共字符串CommentText {获得;组; } 公众诠释ID {获得;组; } [ForeignKey的(ID)] 公开交易DelNav {获得;组; } } 公共类DealDBContext:的DbContext { 公共DealDBContext():基地(DealDBContext){} 公共DbSet<新政>优惠{获得;组; } 公共DbSet<注释>评论{获得;组; } } }解决方案
首先添加模型:
公共类目录 { 公众诠释ID {获得;组; } 公众诠释cateName {获得;组; } }在交易类:
公共类交易 { // .. [ForeignKey的(CATID)] 公共虚拟目录分类{获取;组; } }在启用迁移你应该在控制台管理使用此命令更新数据库:
更新数据库I'm new to mvc. I've creted an MVC app, in which i have used code first approach. Right now i have two tables Deal and Comment. Now i want to add a new table Category in the database and new column categoryId in Deal table.
How i can update database and model?
I'm using Sql Server 2008 R2 for Database.
I've following structure of class:
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; } } }解决方案
first add your model :
public class Category { public int ID { get; set; } public int cateName { get; set; } }in Deal class :
public class Deal { //.. [ForeignKey("CatId")] public virtual Category Category { get; set; } }after Enable Migration you should use this command in console manager to update your database :
update-database
更多推荐
如何更新模型和数据库,在Asp.net MVC code第一种方法
发布评论