簡體   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