Entity framework 我的类中是否可以有一个使用实体框架的bd表中不存在的属性?

Entity framework 我的类中是否可以有一个使用实体框架的bd表中不存在的属性?,entity-framework,Entity Framework,我的数据库中有一个表,如下所示: Table_A Col1 nvarchar(100) Col2 nvarchar(100) Col3 nvarchar(100) 现在在我的实体中,我有: public class Table_A { public string Col1 {get; set;} public string Col2 {get; set;} public string Col3 {get; set;} public string Col4 {ge

我的数据库中有一个表,如下所示:

Table_A
Col1 nvarchar(100)
Col2 nvarchar(100)
Col3 nvarchar(100)
现在在我的实体中,我有:

public class Table_A
{
    public string Col1 {get; set;}
    public string Col2 {get; set;}
    public string Col3 {get; set;}
    public string Col4 {get; set;}
}
使用EF,在某些情况下,我需要执行一个存储过程,返回Col1、Col2、Col3和Col4Col4,col4是与其他表的左外部联接中的列检索;问题是,当我不使用该SP时,Col4会出错

我正在使用EF Core 2.2.3和MS SQL Server 2017

我的类中是否可以有一个使用实体框架的bd表中不存在的属性

是的,你可以使用

摘录:

public class Contact
{
    public int ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [NotMapped]
    public string FullName => $"{FirstName} {LastName}";
    public string Email { get; set; } 
}
或者您可以使用fluentapi

摘录:

public class SampleContext : DbContext
{
    public DbSet<Contact> Contacts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Contact>().Ignore(c => c.FullName);
    }
}
public class Contact
{
    public int ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName => $"{FirstName} {LastName}";
    public string Email { get; set; } 
}
使用EF,在某些情况下,我需要执行一个存储过程,返回Col1、Col2、Col3和Col4Col4,col4是与其他表的左外部联接中的列检索;问题是,当我不使用该SP时,Col4会出错。我正在使用EF Core 2.2.3和MS SQL Server 2017。谢谢


但这回避了一个问题,这是你真正的问题:我可以拥有一个可选的属性吗?答案是否定的。财产也是;始终必须映射或从不映射。

是否尝试添加属性?是的,很遗憾,当我使用返回Col4的SP时,属性中的值为null。