问题
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");
}
}
}
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);
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?
回答1:
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.
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.
来源:https://stackoverflow.com/questions/31948736/do-not-create-existing-table-during-migration