EF Core-我可以使Entity Framework仅映射到数据库中的特定列吗? [英] EF Core - Can I make Entity Framework map only to only specific columns in the database?

查看:40
本文介绍了EF Core-我可以使Entity Framework仅映射到数据库中的特定列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用EF Core 2.0.

I am using EF Core 2.0 in my project.

我的表架构如下:

表格:报告

Id int
Name varchar
Description varchar
<ExtraColumn> <sometype>

我的模型类可能像这样:

And my model class would probably be like this:

class Report
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public <sometype> <SomeProperty> { get; set; }
}

在我的项目中,我不想在EF映射的表中使用某些列.因此,我想将其从实体映射中排除.

In my project, I don't want to use some columns in the table in the EF mapping. So, I would like to exclude it from the entity mapping.

类似地,我想将模型类中的某些属性用于其他内部目的(而不是用于EF映射).

Similarly, I want to use some properties in the model class for other internal purposes (not for EF mapping).

这有可能吗?

P.S.我听说EF Core中的 Ignore()方法满足了我的第二个要求.但是,第一个呢?

P.S. I have heard that the Ignore() method in EF Core addresses the second requirement of mine. But, what about the first one?

有没有出路?

推荐答案

按照惯例,模型中将包含带有getter和setter的公共属性.

By convention, public properties with a getter and a setter will be included in the model.

您可以使用数据注释或Fluent API从模型中排除属性.

You can use Data Annotations or Fluent API to exclude a property from the model.

数据注释:

class Report
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    [NotMapped]
    public <sometype> <SomeProperty> { get; set; }
}

Fluent API:

Fluent API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Report>()
        .Ignore(b => b.<SomeProperty>);
}

这篇关于EF Core-我可以使Entity Framework仅映射到数据库中的特定列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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