c#MVC 5:如何将新表添加到现有数据库

时间:2016-06-20 12:17:32

标签: c# asp.net-mvc entity-framework visual-studio-2015 database-migration

我想在我的数据库中添加一个我的MVC 5 Project生成的新表(包括:adoNetRolesadoNetUser等表..)..我希望我的表有用户的两个外键..这是POCO-class的样子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;

namespace GameApp.Models
{
    [Table("Invitation")]
    public class Invitation 
    {
         public string Host { get; set; }
         public string Invitee { get; set; }
    }

    public class InvitationContext : DbContext
    {
        public InvitationContext()
        {
            if (Database.Exists())
            {
                Database.Initialize(true);
            }
        }
        public DbSet<Invitation> Inv { get; set; }
    }
}

我真的不知道在哪里放置此代码以及如何设置外键。我已启用CodeFirst-Migrations并知道它是如何工作的。如果我要创建自己的项目和数据库,我知道这一切是如何工作的......但mvc5项目让我感到困惑。请帮助我,因为谷歌无法帮助我!

2 个答案:

答案 0 :(得分:1)

我认为&#34;现有数据库&#34;有点扔人。您正在调用现有数据库&#34;似乎只是Identity生成的表,作为应用程序的一部分。换句话说,整个事情是Code First,但您已经完成了初始迁移。

带有单独身份验证的默认MVC 5项目为您提供开箱即用的上下文ApplicationDbContext和用户实体ApplicationUser。那么,你只需要扩展它们。也就是说,您将向DbSet添加新的ApplicationDbContext,而不是创建新的上下文(InvitationContext)。一般来说,一个上下文==一个数据库。如果您希望InvitationApplicationUser进行互动,则他们都需要位于相同的上下文中。然后,您将向Invitation添加外键。如果我理解这种关系:用户有很多邀请(作为主持人),邀请有一个主持人,用户有很多邀请(作为被邀请者),邀请有一个被邀请者。换句话说,你有一个&#34;用户&#34;每个邀请,导致两个单独的一对多关系到同一个表。

public class Invitation
{
    [Key]
    public int Key { get; set; }

    [ForeignKey("Host")]
    public string HostId { get; set; }
    public virtual ApplicationUser Host { get; set; }

    [ForeignKey("Invitee")]
    public string InviteeId { get; set; }
    public virtual ApplicationUser Invitee { get; set; }
}

答案 1 :(得分:0)

假设您已经知道代码优先迁移的工作方式,那么您的代码缺少什么:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data.Entity; 
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GameApp.Models 
{ 
   [Table("Invitation")] 
   public class Invitation 
   { 
      [Key]
      public int Key { get; set; }
      public string Host { get; set; } 
      public string Invitee { get; set; } 
   }

   public class InvitationContext : DbContext
   {
      public InvitationContext() : base("YourConnectionString")
      { }

      public DbSet<Invitation> Inv { get; set; }
   }
}

我强烈建议您将不同文件中的表和上下文分开,以提高可维护性。 理解我现在做了什么:

//I don't think you need this line and if you need it the condition should have a !
if (Database.Exists())
{
    Database.Initialize(true);
}

//The constructor of DbContext can be empty, but it facilitates your work if you pass the connection string
public InvitationContext() : base("YourConnectionString")

//Use the anotation Key say to code-first migrations what is your, well, your key. Se set one key and then migrate your database.
[Key]
public int Key { get; set; }

它主要是代码优先于MVC的问题,只是一些调整,你准备好了。

来源:http://www.dotnet-tricks.com/Tutorial/entityframework/R54K181213-Understanding-Entity-Framework-Code-First-Migrations.html

相关问题