0

I have a .net core 6.0 web api project. I am using entity framework code first. I am trying to login with sa user. I set a password for sa user on sql server management and I am able to login with sa user and password on sql server managment. But I set the same username(sa) and password I am not able to login. So the database is not create. I have received a login failed message.

my appsettings.json

{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": "*","ConnectionStrings": {"DefaultConnection": "Data Source=localhost;Database=userdddd;Persist Security Info=True;User Id=SA;Password=YXZakamoz21?-*54;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=10;"}}

My userDbcontex

namespace ffff.Userwebservice.DbContex{public class UserDbContext : DbContext{public UserDbContext(DbContextOptions<UserDbContext> options) : base(options){}
    DbSet<Users> Users { get; set; }
    DbSet<AccessGrants> AccessGrants { get; set; }
}
}

My program.cs

using ffff.Userwebservice.Repository;using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddDbContext<UserDbContext>(options =>options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"), sqlOptions =>{sqlOptions.EnableRetryOnFailure(maxRetryCount: 5,maxRetryDelay: TimeSpan.FromSeconds(3),errorNumbersToAdd: null);}).EnableSensitiveDataLogging());
var app = builder.Build();
// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment())if (app.Environment.IsDevelopment()){app.UseSwagger();app.UseSwaggerUI();}
app.UseHttpsRedirection();
//app.UseAuthorization();
app.MapControllers();
app.Run();

I tryed many type of the connection. But I could not logined with entity framework. enter image description here

  • Please copy/paste the EXACT ERROR MESSAGE. And please confirm your app is co-located with your DB - that both are running on localhost. – paulsm4 Jul 25 '23 at 22:56
  • Hello, Thanks a lot for answer. Run Add-Migration and Update-Database The database is created. The problem is solved. – Tekin Bozyel Jul 26 '23 at 14:54
  • Q: So it sounds like you're using Entity Framework, you're doing "code first" ... and, since you never did a migration, your app was trying to connect to a database that didn't exist yet. Whoops. Glad you got it resolved. Again: going forward, please remember to include the EXACT ERROR MESSAGE in your question! – paulsm4 Jul 26 '23 at 15:52

0 Answers0