Property for contacting fields in query LINQ to Entities

c# entity-framework entity-framework-6 linq-to-entities

Question

I have in Database table Users:

pk firstName lastName
1  Mary      Snow
...

LINQ to Entities create class:

public partial class User
{
   public int pk { get; set; }
   public string firstName { get; set; }
   public string lastName { get; set; }
}

I tried to create new part of this class

public partial class User
{
   public string FullName 
   {
      get
      {
        return lastName + " " + firstName;
      }
   }
}

And after i tried to use it in IQueryable i get exception "The specified type member 'FullName' is not supported in LINQ to Entities". Can i create property or extension method for this?

I need it in many parts of my code. And it is not comfortable for me use in every query lastName + " " + firstName. I don't understand why i can use in IQueryable "where(x=> (x.lastName + " " + x.firstName) == "Mary Show")" but i can't write new property and use it as "where(x=> x.FullName == "Mary Show")".

It is example in really i have more modifications of fields.

upd: I use .NET Framework 4.5.2 and Entity Framework 6.

upd2: I use Database-First approach.

1
2
3/11/2020 3:20:12 AM

Popular Answer

You can alternatively do this with Fluent API overriding OnModelCreating function in your DBContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<User>().Ignore(t => t.FullName );
   base.OnModelCreating(modelBuilder);
}
0
3/6/2020 3:27:03 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow