سؤال

public class baseEntity
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}


public class ProjectEntity : baseEntity
{
    public string Address { get; set; }
    public string PhoneNo { get; set; }
}


public class ProcessEntity : baseEntity
{
    public string TypeName { get; set; }
    public int Steps { get; set; }
}

public class DBContext : DbContext
{
    public DBContext() : base("DefaultConnection") 
    {

    }
    public DbSet<baseEntity> BaseEntities { get; set; }

}

DBContext db = new DBContext();
var list = from p in db.BaseEntities select p

this will get all fields from baseentity .whatever it is BaseEntity, ProcessEntity or ProjectEntity.

I want to get the fields only in BaseEntity (only ID,Name),How can I do ?

var list = from p in db.BaseEntities select new {p.ID,p.Name}

this is not what I want.Because there are a lot of fields in my project. I don't like to write the code like this

var list = from p in db.BaseEntities select new {p.ID,p.Name,p.xxx .....................}
هل كانت مفيدة؟

المحلول

Try:

var list = from p in db.BaseEntities.OfType<baseEntity>() select p

Yet, keep in mind the actual Sql query to be generated depends on your Inheritance Strategy (TPT/TPH/TPC).

نصائح أخرى

ofType can finish this job.thank you. But there are more Fields and more Inheritance class will add to this project.

var list = from p in db.BaseEntities.OfType<baseEntity>() select p

will generate a very complex sql. I try list.toString() to get the sql and copy to the SQL Server Management Studio , the content is more than 8000 lines. How to improve the performance ?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top