迁移期间不创建现有表 [英] Do not create existing table during Migration

查看:91
本文介绍了迁移期间不创建现有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体通过代码优先迁移连接到现有数据库.在数据库中,我需要连接到一些已经存在的表并添加一些新表.我以为我已经弄清楚了,但是它仍在尝试创建已经存在的表.这是其中一个表的模型:

I am using entity to connect to an existing database using code first migrations. In the database I need to connect to a few tables that already exist and add a few new ones. I thought I had it figured out but it is still trying to create the tables that already exist. Here is the model of one of the tables:

namespace PTEManager.Domain
{
    public partial class OpsUser
    {
        public int u_user_id { get; set; }

        public Guid DepartmentID { get; set; }

        public string email_addr { get; set; }

        public string first_nme { get; set; }

        public string last_nme { get; set; }

        public Guid msrepl_tran_version { get; set; }

        public string status { get; set; }

        public string user_nme { get; set; }

        public int u_branch_id { get; set; }

    }
}

这是我将模型映射到表格的地方:

Here is where I map that model to the table:

namespace PTEManager.Domain.Mapping
{
    class UserMap : EntityTypeConfiguration<OpsUser>
    {
        public UserMap()
        {
            // Primary Key
            this.HasKey(t => t.u_user_id);

            // Properties
            this.Property(t => t.DepartmentID);

            this.Property(t => t.email_addr)
                .IsRequired()
                .HasMaxLength(50);

            this.Property(t => t.first_nme)
                .IsRequired()
                .HasMaxLength(30);

            this.Property(t => t.last_nme)
                .IsRequired()
                .HasMaxLength(30);

            this.Property(t => t.msrepl_tran_version)
                .IsRequired();

            this.Property(t => t.status)
                .IsRequired()
                .HasMaxLength(1);

            this.Property(t => t.user_nme)
                .IsRequired()
                .HasMaxLength(15);

            this.Property(t => t.u_branch_id)
                .IsRequired();

            // Table & Column Mappings
            this.ToTable("Users");
            this.Property(t => t.u_user_id).HasColumnName("u_user_id");
            this.Property(t => t.DepartmentID).HasColumnName("DepartmentID");
            this.Property(t => t.email_addr).HasColumnName("email_addr");
            this.Property(t => t.first_nme).HasColumnName("first_nme");
            this.Property(t => t.last_nme).HasColumnName("last_nme");
            this.Property(t => t.msrepl_tran_version).HasColumnName("msrepl_tran_version");
            this.Property(t => t.status).HasColumnName("status");
            this.Property(t => t.user_nme).HasColumnName("user_nme");
            this.Property(t => t.u_branch_id).HasColumnName("u_branch_id");
        }
    }
}

这是我的dbContext:

Here is my dbContext:

namespace PTEManager.Domain.Data
{
    public class DataContext : DbContext, IDataContext
    {
        static DataContext()
        {
            Database.SetInitializer<DataContext>(null);
        }

        public DataContext()
            : base("OPSPROD")
        {
        }


        /// 
        public DbSet<OpsUser> OpsUsers { get; set; }

        public DbSet<Package> Packages { get; set; }

        public DbSet<PTEInteractiveCourse> PTEInteractiveCourses { get; set; }

        public DbSet<PTETrackingClass> PTETrackingClasses { get; set; }

        public DbSet<STCIProductInteractiveInfo> STCIProductInteractiveInfos { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Configurations.Add(new UserMap());
            modelBuilder.Configurations.Add(new PackageMap());
            modelBuilder.Configurations.Add(new STCIProductInteractiveInfoMap());
        }
    }

}

但是,当我添加新的迁移时,它仍然会创建此迁移:

However, when I add a new migration it still creates this:

CreateTable(
                "dbo.Users",
                c => new
                    {
                        u_user_id = c.Int(nullable: false, identity: true),
                        DepartmentID = c.Guid(nullable: false),
                        email_addr = c.String(nullable: false, maxLength: 50),
                        first_nme = c.String(nullable: false, maxLength: 30),
                        last_nme = c.String(nullable: false, maxLength: 30),
                        msrepl_tran_version = c.Guid(nullable: false),
                        status = c.String(nullable: false, maxLength: 1),
                        user_nme = c.String(nullable: false, maxLength: 15),
                        u_branch_id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.u_user_id);

只是确保我尝试运行update-database并告诉我

Just to make sure I try running update-database and it tells me

数据库中已经有一个名为用户"的对象.

There is already an object named 'Users' in the database.

在这里,我找到了有关映射到现有表的信息:

this is where I found the information on mapping to an existing table:

http://www.codeproject.com/Tips/661053/Entity-Framework-Code-First-Map

如何防止它尝试创建现有表并仅映射到该表?

How do I prevent it from trying to create the existing table and only map to it?

推荐答案

这是Entity Framework本身的限制,您无法解决.为了使迁移正常运行,他们必须了解您的整个数据库架构,这意味着他们必须对您的整个数据库架构负责.您不能迁移部分实体,而不能迁移其他实体.

This is a limitation of Entity Framework, itself, that you can't workaround. In order for migrations to function correctly, they must know about your entire database schema, which means they must be responsible for your entire database schema. You can't migrate part of your entities and not others.

如果您有一个需要使用现有表的用例,则应将它们移至单独的数据库并使用两个上下文:一个用于Entity Framework所负责的实体,将被迁移至该上下文;而另一种则只是连接到您现有的数据库,并且已禁用迁移.

If you have a use case where you need to work with existing tables, then you should move those off to a separate database and use two contexts: one for your entities that Entity Framework is responsible for, which will be migrated against, and one that simply connects to your existing database, and has migrations disabled.

从技术上讲,我认为您可以在两个上下文中使用相同的数据库,只要您将实体正确隔离即可.但是,这就是麻烦所在:当相关实体(外键)位于同一数据库中时,尝试它们是太诱人了.如果您在迁移上下文中的实体与非迁移上下文中的实体之间创建了一种关系,则迁移上下文实际上也将假定该实体的所有权,并尝试创建表.然后,您就回到同一条船上.通过为两个不同的上下文使用两个单独的数据库,可以确保不会流血.

Technically, I think you could use the same database for both contexts, as long as you kept the entities properly segregated. However, in that lies the rub: it's far too tempting to try to related entities (foreign keys) when they're in the same database. If you create a relationship between an entity in your migration context to one in your non-migration context, the migration context will actually assume ownership of that entity as well, and try to create the table. Then, you're right back in the same boat. By using two separate databases for your two different contexts, you'll be assured that there's no bleed over.

这篇关于迁移期间不创建现有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆