I have a DbSet which database object has the key of multiple column with expression. For example, the database object (view or table) has the key of cast(Time as Date), Col2
(Time has the type of DateTime
). (I'm not the owner of the SQL Server database)
However, the following way to setup the key got error.
public class MyDbContext : DbContext
{
public DbSet<MyEntity> MyEntity { get; set; }
protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<MyEntity>().HasKey( x => new { x.Time.Date, x.Col2 } ); // Error
The database object (Table or View) can have the unique constraint of
cast(Time as Date), Col2
COL2 has the type of varchar(50)
.