如何首先将新表添加到现有数据库代码

编程入门 行业动态 更新时间:2024-10-24 06:37:52
本文介绍了如何首先将新表添加到现有数据库代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

最初我使用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; 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.

如果没有,你将需要运行启用迁移 - 启用自动移动 或许可以先阅读一下这里: msdn.microsoft/en-gb/data/jj554735.aspx

If you haven't, you will need to run Enable-Migrations –EnableAutomaticMigrations Perhaps do some further reading here first though: msdn.microsoft/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.

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

更多推荐

如何首先将新表添加到现有数据库代码

本文发布于:2023-10-27 02:58:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   数据库   将新表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!