Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does not contain definition for "HasDefaultValue"

I want to set a default value for a non nullable field in code first.

I have included Microsoft.EntityFrameworkCore but I'm still getting

Does not contain definition for 'HasDefaultValue'

Using EF Core 2.2.6

using Microsoft.EntityFrameworkCore;

public partial class MyDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Agency>(entity =>
        {
                entity.HasOne(d => d.Region)
                    .WithMany(p => p.Agencies)
                    .HasForeignKey(d => d.RegionId)
                    .HasDefaultValue(1)
                    .HasConstraintName("FK_Agency_Region");
        });
    }
    ...
}
like image 699
Abhishek Chandel Avatar asked Mar 15 '26 14:03

Abhishek Chandel


1 Answers

EF Core 6+

My issue here was I didn't add this package to my Repository project:

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/

PM> Install-Package Microsoft.EntityFrameworkCore.Relational 
like image 123
Sampath Avatar answered Mar 17 '26 04:03

Sampath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!