When I want to add controller using ASP.NET Core MVC with views:
This is my DbContext
class:
namespace Infrastructure
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<Owner> owners { get; set; }
public DbSet<ProtoFile> protoFiles { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Owner>().Property(x => x.Id).HasDefaultValueSql("NEWID()");
modelBuilder.Entity<ProtoFile>().Property(x => x.Id).HasDefaultValueSql("NEWID()");
modelBuilder.Entity<Owner>().HasData(
new Owner
{
Id = Guid.NewGuid(),
Avatar = "avatar.jpg",
FullName = "Mohammad AlMohammad AlMahmoud",
Profile = ".NET Full Stack Developer"
});
}
}
}
And I have get this error:
CodePudding user response:
you have to add this code to your program class (if you use net 6)
builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
or if your are using net 5 or lower, add the same to a startup class
services.AddDbContext<DataContext>(.....
and remove
base.OnModelCreating(modelBuilder);
CodePudding user response:
I solved this problem when I added this code into Datacontext class enter image description here
from Microsoft docs https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/