efcore: is HasComputedColumnSql bug or wrong way to use?

Include your code

public class User:
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

       // not want   store  this Column , but   need it    for  search  or  linq  Select Opt
        [NotMapped]
        public string FullName { get; set; }

    }
// false:   the value is computed when the value is read, and does not occupy any actual storage
  modelBuilder.Entity<User>().Property(t => t.FullName).HasComputedColumnSql("[LastName] + ' / ' + [FirstName]",false);

// want  wirte like this  
_userRepository.Where(t => t.FullName .Contains("CC")).First()

_userRepository.Select(t => t.FullName).First()

Include verbose output

Microsoft.Data.SqlClient.SqlException (0x80131904): 列名 'FullName' 无效。

Include provider and version information

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 6.0) Operating system: IDE: (e.g. Visual Studio 2022)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 24 (12 by maintainers)

Most upvoted comments

[NotMapped] tells EF that the column doesn’t exist in the database, but it sounds like you do want it to exist in the database as a computed column.

@michiproep I’d be very wary of going down that route; doing this properly - without introducing subtle bugs - is probably going to be difficult and require good knowledge of the query pipeline (and may also involve access to internal APIs which change across releases). I’d advise on finding another way.

@michiproep please open a new issue with a minimal, runnable code sample - it’s impossible to know from the above exactly what’s going on. I’d advise checking check the SQL being executed when you apply your migration.

I think you’re looking for #10768