本文介绍了如何在ASP.net MVC中的代码优先方法中重命名主键列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF代码优先方法.我需要重命名我的PK列名称.

I am using EF code first approach. I need to rename my PK column name.

 public class classname
    {
    [Key]
    public int OldId { get; set; }
    }

我需要将OldId更改为NewId.我尝试将OldId重命名为NewId,尝试使用Migration更新数据库,但这无济于事.我尝试重命名列名和模型,并再次尝试更新数据库时仍然出现错误.如何重命名主键列而不丢失其数据?

I need to change OldId to NewId. I tried renaming my OldId to NewId, I tried updating my database with Migration but that didn't help.I tried renaming both in column name and model and again when I tried to update my database I still get error.How can I rename my primary Key column without losing its data?

推荐答案

是您的代码中不再使用OldId,而是仍在数据库中的问题,还是您的问题,即OldId在您的代码中但具有您数据库中的其他列名不同?

Is the question that OldId is not used in your code anymore but is still in your database, or is your problem that it OldId is in your code but has a different column name in your database?

如果要告诉实体框架,您的 DbSet 中的属性名称在数据库中具有不同的列名称,则可以使用流利的API或数据批注:

You can use fluent API or data annotations if you want to tell entity framework that the name of property in your DbSet has a different column name in the database:

流式API方法

public class MyDbContext : DbContext
{
    public DbSet<ClassName> ClassNames {get; set;}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         // Tell entity framework that you are now building ClassName:
         var classNameConfig = modelBuilder.Entity<ClassName>();

         // ClassName has an int primary key in OldId:
         classNameConfig.HasKey(className => className.OldId);

         // "columnX" in the database corresponds with property OldId
         classNameConfig.Property(className => className.OldId)
             .HasColumnName("columnX");
    }
}

当然,在此特定示例中,可以将其合并为一个语句.

Of course in this specific example this can be concatenated into one statement.

关于Fluent API的好处是,您可以将类与实际的数据库表示形式断开连接.这向DbSet的用户隐藏了实际数据库如何命名其列,列顺序,最小大小,最大大小等.

The nice thing about fluent API is that you disconnect your classes from the actual database representation. This hides how the actual database names its columns, column order, min size, max size etc from the user of the DbSets.

只需指定不同的DbContext,就可以将相同的类与不同的Db一起使用.

Just specify a different DbContext, and you can use the same classes with a different Db.

例如,如果要在不同的数据库中使用相同的实体框架类:

For instance, if you want to use the same entity framework classes in different databases:

class BaseContext : DbContext
{
    public DbSet<MyClass> MyClasses {get; set;}
}

// context to be used with database X:
class DataBaseXContext : BaseContext
{
     protected override void OnModelCreating(
DbModelBuilder modelBuilder)
     {
         // I want to use "columnX" for property A, which is optional:
         modelBuilder.Entity<MyClass>
             .Property(p => p.A)
             .HasColumnName("columnX")
             .IsOptional();
     }
}

// context to be used with database Y:
class DataBaseXContext : BaseContext
{
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
         // I want to use "columnY" for property A, which is required:
         modelBuilder.Entity<MyClass>
             .Property(p => p.A)
             .HasColumnName("columnY")
             .IsRequired();
     }
}

数据注释方法

如果您完全确定只希望在具有一个预定义模型的一个数据库中使用类,则可以使用数据注释.

If you are absolutely sure you want your classes to be used in only one database, with one pre-defined model, you could use Data Annotations.

但是,您的问题表明,在不同的数据库模型中使用相同的类并不罕见.因此,尽管这种方法非常常用,但我还是建议您重新考虑使用数据注释的决定.

However, your question shows that it is not uncommon that the same classes will be used in a different database model. So, although this method is very commonly used, I urge you to reconsider a decision to use Data Annotations.

public class classname
{
    [Key]
     [Column("ColumnX")]
     public int OldId { get; set; }
}

这篇关于如何在ASP.net MVC中的代码优先方法中重命名主键列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 03:06