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>);
}