我有一个.net core 6.0 Web api 项目。我首先使用实体框架代码。 我正在尝试使用 sa 用户登录。我在sql server管理上为sa用户设置了密码,并且我可以在sql server管理上使用sa用户和密码登录。但我设置了相同的用户名(sa)和密码我无法登录。所以数据库没有创建。我收到登录失败的消息。
我的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;"}}
我的用户Dbcontex
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; }
}
}
我的程序.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();