'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");
});
}
...
}
Solution 1:[1]
Figured the correct syntax. Was assigning the default value at wrong place.
entity.Property(e => e.RegionId).HasDefaultValue(1)
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abhishek Chandel |
Solution 2 | Sampath |