忽略数据模型中的属性,同时将其保留在EF Core迁移中 [英] Ignore properties in data model while keeping them in EF Core migrations

查看:269
本文介绍了忽略数据模型中的属性,同时将其保留在EF Core迁移中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用EF Core的未开发应用程序,该应用程序必须与旧数据库通信。我希望EF忽略数据库中的某些列,因为它们最终将被弃用,并且我不希望它们出现在新的实体模型中。我无法删除它们,因为旧版系统仍依赖它们。

I am creating a greenfield application that uses EF Core which must talk to a legacy database. I want EF to ignore some of the columns in the database because they will eventually be deprecated and I don't want them in the new entity model. I can't remove them yet as the legacy system still relies on them.

对于一个名为 DeprecatedFeature ,我想执行以下操作:

For an unwanted database column called DeprecatedFeature, I want to do something like:

modelBuilder.Entity<MyEntity>(entity => {
   entity.HasKey(t => t.Id);
   entity.ToTable("MyEntity");
   entity.ColumnIgnore("DeprecatedFeature"); // <-- this is what I want to do
})

现在,我能做的最好的就是包括财产和商标

Right now, the best I can do is include the property and mark it as obsolete:

public class MyEntity 
{
    public int Id { get; set; }

    [Obsolete("Deprecated in latest version")]
    public string DeprecatedFeature { get; set; }
}

但这意味着我无法打开警告为错误。我仍然需要在数据库上运行迁移。

But that means I can't turn on "warnings as errors". I still need to run migrations on my database.

类似的问题: EF 4.x EF核心加载时跳过列使用EF Designer / EDMX 重复

我可以从答案中看到我的问题有些困惑:

I can see by the answers that there is some confusion about my question:

NotMapped 在模型中具有您不希望在数据库中使用的属性。我的问题是相反的。我的数据库中有我不想在模型中使用的列。

NotMapped is used when you have a property in your model that you don't want in the database. My problem is the other way around. I have a column in my database that I don't want in my model.

推荐答案

您有两种选择:


  1. 使用 NotMappedAttribute

public class MyEntity
{
     public int Id { get; set; }    
     [NotMapped]
     public string DeprecatedFeature { get; set; }
}


  • 使用FluentAPI:

  • Using FluentAPI:

    modelBuilder.Entity< MyEntity>()。Ignore(c => c.DeprecatedFeature);

    这篇关于忽略数据模型中的属性,同时将其保留在EF Core迁移中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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