简体   繁体   English

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

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

I want to add a new table in my database which my MVC 5 Project generated (with tables like: adoNetRoles , adoNetUser etc..).. I want my table to have two foreign keys on a user.. here is what my POCO-class looks like: 我想在我的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; }
    }
}

I really don't know where to put this code and how to set the foreign key. 我真的不知道该代码放在哪里以及如何设置外键。 I enabled CodeFirst-Migrations already and know how it works. 我已经启用了CodeFirst-Migrations并且知道它是如何工作的。 I know how all this works if I would create my own project and database.. but the mvc5 project confuses me. 我知道如果我创建自己的项目和数据库,所有这些工作如何。但是mvc5项目使我mvc5困惑。 Please help me because Google couldn't! 请帮助我,因为Google无法!

I think the "existing database" bit is throwing people. 我认为“现有数据库”有点让人讨厌。 What you're calling an "existing database" seems to be simply the tables generated by Identity, as part of your application. 您所谓的“现有数据库”似乎只是Identity生成的表,它们是应用程序的一部分。 In other words, the whole thing is Code First, but you've already done the initial migration. 换句话说,整个过程是“代码优先”,但是您已经完成了初始迁移。

The default MVC 5 project with individual auth gives you, among other things, a context, ApplicationDbContext , and a user entity, ApplicationUser , out of the box. 具有单独身份验证的默认MVC 5项目为您提供了现成的上下文ApplicationDbContext和用户实体ApplicationUser You will simply, then, just extend these. 然后,您将简单地扩展这些内容。 Namely, you will add a new DbSet to ApplicationDbContext , rather than creating a new context ( InvitationContext ). 即,您将向ApplicationDbContext添加一个新的DbSet ,而不是创建一个新的上下文( InvitationContext )。 Generally speaking, one context == one database. 一般来说,一个上下文==一个数据库。 If you want Invitation and ApplicationUser to interact, then they both need to be in the same context. 如果要InvitationApplicationUser进行交互,则它们都需要处于同一上下文中。 Then, you will add a foreign key(s) to Invitation . 然后,将外键添加到Invitation If I understand the relationship: user has many invitations (as host) and invitation has one host, and user has many invitations (as invitee) and invitation has one invitee. 如果我了解这种关系:用户有很多邀请(作为主持人),邀请有一个主持人,用户有很多邀请(作为受邀者),邀请有一个受邀者。 In other words, you've got two foreign keys to a "user" per invitation, resulting in two separate one-to-many relationships to the same table. 换句话说,每个邀请中有两个外键指向一个“用户”,从而导致到同一个表的两个单独的一对多关系。

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

Assuming you already know how Code-first migrations works here what is missing for your code: 假设您已经了解了“代码优先”迁移的工作原理,那么代码中缺少什么:

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 highly recomend you to separate tables and context in diferent files for better maintainability. 我强烈建议您将不同文件中的表和上下文分开,以实现更好的可维护性。 Understanding what I did now: 了解我现在所做的事情:

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

It mostly problems with code-first than MVC, just some adjusts and you're ready to go. 与MVC相比,代码优先主要存在问题,只需进行一些调整即可开始使用。

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

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

相关问题 如何在 c# 中的现有数据表中添加新列 - how to add a new column to an existing data table in c# 如何使用现有数据库在MVC(C#)中进行开发 - How to develop in MVC (C#) with existing database 如何在运行时使用C#MVC在运行时向现有数据表添加新行 - How to add new row to an already existing DataTable on Runtime in C# MVC on Runtime 如何使用 .net5 从 MVC C# 控制器向现有 Microsoft 身份用户添加新角色 - How to add new role to existing Microsoft Identity User from MVC C# Controller using .net5 如何在 web api c# entity core 2.14 中向现有数据库添加新表 - how to add a new table to an existing db in web api c# entity core 2.14 如何使用Word Interop将新行添加到C#中的现有Word文档表中? - How to add new rows to an existing word document table in C# using Word Interop? 如何使用c#sql server向现有表添加新行 - How to add a new row to an existing table using c# sql server 在mvc4中向现有数据库添加新表 - Adding a new table to existing database in mvc4 如何在C#中向现有XML添加新的XmlElement? - How to add a new XmlElement to existing XML in C#? 如何将新项目添加到现有列表 c#? - How to add new Items to an Existing List c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM