本文介绍了如何首先将新表添加到现有数据库代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我使用EF 6代码来创建一个新的数据库和两个新的表。代码是:

Originally I used EF 6 code first to create a new database and two new tables. The code is:

 public class TestingContext : DbContext, IDisposable
{
    public DbSet<CallDataRecord> CallDataRecords { get; set; }
    public DbSet<Attempt> Attempts { get; set; }

    public TestingContext()
        : base("Testing")
    {
        Database.SetInitializer<TestingContext>(new MigrateDatabaseToLatestVersion<TestingContext, GenericIVR.Migrations.Configuration>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Attempt>().HasRequired(t => t.CallDataRecord).WithMany(a => a.Attempts).HasForeignKey(t => t.FKTaskId);

        modelBuilder.Entity<Attempt>().Property(x => x.AttemptId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();

        modelBuilder.Entity<CallDataRecord>().Property(x => x.TaskId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
    }
}

现在我的策略改变了,我不想到一个新的DB。我想将新的表添加到现有的数据库,例如 DevDB

Now my strategy is changed, I don't want to a new DB. I want to add the new tables to an existing DB, say DevDB.

如何更改代码?我必须先使用逆向工程代码?

How to change the code? DO I have to use Reverse Engineering Code First?

更新:
连接字符串为:

UPDATED:The connection string is:

<connectionStrings>
<add name="Testing" connectionString="Data Source=dddd.corporate.xxxx.com; Initial Catalog=Testing; User ID=sa; Password=password; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

推荐答案

如果你有自动迁移设置,这应该是很直接的。

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

如果没有,你将需要运行启用迁移 - 启用自动移动
或许可以先阅读一下这里:

If you haven't, you will need to run Enable-Migrations –EnableAutomaticMigrationsPerhaps do some further reading here first though: http://msdn.microsoft.com/en-gb/data/jj554735.aspx

对于任何希望使用新表更新数据库的用户(例如,我想添加一个UserAttachment表与我现有的用户表一起使用)首先使用EF代码,请执行以下操作:

For anyone looking to update a database with a new table (say I want to add an UserAttachment table to sit alongside my existing User table) using EF code first, do the following:

启用自动迁移后,您应该确保...

With automatic migrations enabled you should ensure you have...

1。)根据您的需要创建新的模型。

1.) Create your new model as you see fit.

2。)创建您的配置文件,沿着以下行:

2.) Create your Configuration file, something along the lines of:

 class UserAttachmentConfiguration : EntityTypeConfiguration<UserAttachment>
    {
        public UserAttachmentConfiguration()
            : base()
        {
            HasKey(p => p.UserId);
            ToTable("UserAttachment");

            HasRequired(t => t.User)
                .WithOptional(t => t.UserAttachment);
        }
    }

3。)添加您的您的主要 Context.cs 文件

DbSet

public DbSet<UserAttachment> UserAttachment {get; set;}

modelBuilder

modelBuilder.Configurations.Add(new UserAttachmentConfiguration());

4。)运行 update-database via Visual Studio的软件包管理器控制台,确保您从下拉列表中选择了正确的项目,这可能是一个 .Repository named project。

4.) Run update-database via Visual Studio's Package Manager Console, make sure you have selected the correct project from the drop down, this is likely to be a .Repository named project.

您的新表格应该现在存在。

这篇关于如何首先将新表添加到现有数据库代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 00:15