繁体   English   中英

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

[英]c# MVC 5: how to add new table to the existing database

我想在我的MVC 5项目生成的数据库中添加一个新表(带有诸如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项目使我mvc5困惑。 请帮助我,因为Google无法!

我认为“现有数据库”有点让人讨厌。 您所谓的“现有数据库”似乎只是Identity生成的表,它们是应用程序的一部分。 换句话说,整个过程是“代码优先”,但是您已经完成了初始迁移。

具有单独身份验证的默认MVC 5项目为您提供了现成的上下文ApplicationDbContext和用户实体ApplicationUser 然后,您将简单地扩展这些内容。 即,您将向ApplicationDbContext添加一个新的DbSet ,而不是创建一个新的上下文( InvitationContext )。 一般来说,一个上下文==一个数据库。 如果要InvitationApplicationUser进行交互,则它们都需要处于同一上下文中。 然后,将外键添加到Invitation 如果我了解这种关系:用户有很多邀请(作为主持人),邀请有一个主持人,用户有很多邀请(作为受邀者),邀请有一个受邀者。 换句话说,每个邀请中有两个外键指向一个“用户”,从而导致到同一个表的两个单独的一对多关系。

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; }
}

假设您已经了解了“代码优先”迁移的工作原理,那么代码中缺少什么:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM