如何更新模型和数据库,在Asp.net MVC code第一种方法

编程入门 行业动态 更新时间:2024-10-27 16:25:54
本文介绍了如何更新模型和数据库,在Asp MVC code第一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来的 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第一种方法

本文发布于:2023-05-29 23:14:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/355167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:种方法   模型   数据库   net   Asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!