如何在上下文ASP.NET MVC中添加新表 [英] How to add new table in context ASP.NET MVC

查看:113
本文介绍了如何在上下文ASP.NET MVC中添加新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我。
我有一个文件ImageContext:

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;

命名空间userGallery.Models
{
public class ImageContext:DbContext
{
public DbSet< Image>图片{get;组; }
}
}

我创建新的模型幻灯片并添加字符串连接ImageContext。
结果

  ... 
public class ImageContext:DbContext
{
public DbSet< Image>图片{get;组; }
public DbSet< Slide>幻灯片{get;组; }
}
...

如何更新数据库以创建表幻灯片?



屏幕数据库表:
在此输入图像描述

解决方案

如果您有自动迁移设置,这应该很直接。 / p>

如果没有,您需要运行

 启用-Migrations -EnableAutomaticMigrations 

也许先进一步阅读这里: http://msdn.microsoft.com/en-gb/data/jj554735.aspx


  1. 添加幻灯片创建新的迁移



    添加迁移SlideMigration


  2. 现在做一个更新...希望它应该都可以工作。



    <数据库



Please, help me. I had a file ImageContext:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace userGallery.Models
{
    public class ImageContext : DbContext
    {
        public DbSet<Image> Images { get; set; }
    }
}

I create new model Slide and add string connect in ImageContext. Result

...
public class ImageContext : DbContext
    {
        public DbSet<Image> Images { get; set; }
        public DbSet<Slide> Slides { get; set; }
    }
...

How to update database to create a table Slides?

Screenshoot database table: enter image description here

解决方案

If you have automatic migrations set up this should be pretty straight forward.

If you haven't, you will need to run

Enable-Migrations –EnableAutomaticMigrations

Perhaps do some further reading here first though: http://msdn.microsoft.com/en-gb/data/jj554735.aspx

  1. Create a new migration with the addition of Slides

    Add-Migration "SlideMigration"

  2. Now do an update... and hopefully it should all work.

    Update-Database

这篇关于如何在上下文ASP.NET MVC中添加新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆