EF Core - 我是否只能将Entity Framework映射到数据库中的特定列?

时间:2018-03-04 17:51:37

标签: c# .net-core entity-framework-core

我在我的项目中使用EF Core 2.0。

我的表架构有点像这样:

表:Report

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

我的模型类可能是这样的:

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

在我的项目中,我不想在EF映射中使用表中的某些列。所以,我想将它从实体映射中排除。

同样,我想在模型类中使用一些属性用于其他内部目的(不适用于EF映射)。

这有可能吗?

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

有出路吗?

1 个答案:

答案 0 :(得分:2)

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

您可以使用Data Annotations或Fluent API从模型中排除属性。

数据注释:

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:

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