c# - 不使用 ASP.Net Core 2 中的迁移命令 Update-Database 创建数据库

标签 c# asp.net-web-api asp.net-core asp.net-core-2.0

我使用 WebAPI 创建了新的 ASP.Net 核心项目。使用以下链接。

jwt-authentication-with-aspnet-core-2-web-api-angular-5-net-core-identity-and-facebook-login

我必须手动使用 add-migration update-database。

这里我再补充一件事,我不知道如何在 VS Code 中调试应用程序。在 VS Code 中创建我的项目后,我在 Visual Studio 2017 中打开我的项目。

当我要在程序包管理器控制台中运行更新命令 Update-Database 时。在我的理解中,我也遇到了以下错误,上面的命令用于创建数据库并在其中添加迁移表。 (如我错了请纠正我)。

 fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
  Failed executing DbCommand(3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  CREATE TABLE[AspNetRoles] (

     [Id] nvarchar(450) NOT NULL,
     [ConcurrencyStamp] nvarchar(max) NULL,
      [Name] nvarchar(256) NULL,
      [NormalizedName] nvarchar(256) NULL,
      CONSTRAINT[PK_AspNetRoles] PRIMARY KEY([Id])
  );
 System.Data.SqlClient.SqlException(0x80131904): There is already an object named 'AspNetRoles' in the database.

 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)

 at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)

 at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)

 at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)

 at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)

 at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)

 at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

 at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues)

 ClientConnectionId:56b94f04-73ff-4391-87fc-36a6c256c650

 Error Number:2714,State:6,Class:16

 System.Data.SqlClient.SqlException(0x80131904): There is already an object named 'AspNetRoles' in the database.

 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)

 at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)

 at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)

 at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)

 at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)

 at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)

 at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

 at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues)

 at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues)

 at      Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues)


 at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable1 migrationCommands, IRelationalConnection connection)

 at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)

 at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)

 at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()

 at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

 ClientConnectionId:56b94f04-73ff-4391-87fc-36a6c256c650

 Error Number:2714, State:6, Class:16

 There is already an object named 'AspNetRoles' in the database.`

我还在这里分享了我的一些代码,并在 github 上上传了我的应用程序

program.cs

namespace server
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
             WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

    }
}

Startup.cs

public class Startup
{
    private const string SecretKey = "iNivDmHLpUA223sqsfhqGbMRdRj1PVkH"; // todo: get this from somewhere secure
    private readonly SymmetricSecurityKey _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                b => b.MigrationsAssembly("server")));

        services.AddSingleton<IJwtFactory, JwtFactory>();

        // Register the ConfigurationBuilder instance of FacebookAuthSettings
        services.Configure<FacebookAuthSettings>(Configuration.GetSection(nameof(FacebookAuthSettings)));

        services.TryAddTransient<IHttpContextAccessor, HttpContextAccessor>();

        // jwt wire up
        // Get options from app settings
        var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));

        // Configure JwtIssuerOptions
        services.Configure<JwtIssuerOptions>(options =>
        {
            options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
            options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)];
            options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
        });

        var tokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = true,
            ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],

            ValidateAudience = true,
            ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],

            ValidateIssuerSigningKey = true,
            IssuerSigningKey = _signingKey,

            RequireExpirationTime = false,
            ValidateLifetime = true,
            ClockSkew = TimeSpan.Zero
        };

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

        }).AddJwtBearer(configureOptions =>
        {
            configureOptions.ClaimsIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
            configureOptions.TokenValidationParameters = tokenValidationParameters;
            configureOptions.SaveToken = true;
        });

        // api user claim policy
        services.AddAuthorization(options =>
        {
            options.AddPolicy("ApiUser", policy => policy.RequireClaim(Constants.Strings.JwtClaimIdentifiers.Rol, Constants.Strings.JwtClaims.ApiAccess));
        });

        // add identity
        var builder = services.AddIdentityCore<EmployeeProfile>(o =>
        {
            // configure identity options
            o.Password.RequireDigit = false;
            o.Password.RequireLowercase = false;
            o.Password.RequireUppercase = false;
            o.Password.RequireNonAlphanumeric = false;
            o.Password.RequiredLength = 6;
        });
        builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
        builder.AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

        services.AddAutoMapper();
        services.AddMvc().AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseExceptionHandler(
            builder =>
            {
                builder.Run(
                    async context =>
                        {
                            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            context.Response.Headers.Add("Access-Control-Allow-Origin", "*");

                            var error = context.Features.Get<IExceptionHandlerFeature>();
                            if (error != null)
                            {
                                context.Response.AddApplicationError(error.Error.Message);
                                await context.Response.WriteAsync(error.Error.Message).ConfigureAwait(false);
                            }
                        });
            });

        app.UseAuthentication();
        app.UseDefaultFiles();
        app.UseStaticFiles();
        app.UseMvc();
    }
}

ApplicationDbContext.cs

public class ApplicationDbContext : IdentityDbContext<EmployeeProfile>
{
    public ApplicationDbContext(DbContextOptions options)
        : base(options)
    {
    }

    public DbSet<EmployeeProfile> EmployeeProfiles { get; set; }
}

我的问题是我如何使用迁移更新命令创建我的数据库以及我的代码中出了什么问题。

最佳答案

我找到了问题的答案。

我只需要更改以下两个文件中的 DefaultConnection:

appsettings.jsonappsettings.Development.json

之前

"ConnectionStrings": {
"DefaultConnection": "Server=(LocalDb)\\MSSQLLocalDB;Database=webapi;Trusted_Connection=True;MultipleActiveResultSets=true"
}

之后

"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=WebApiEfCore;Trusted_Connection=True;"
}

然后运行以下查询。

dotnet ef migrations add initial
dotnet ef database update

及其工作原理。这是经过验证的快照。

enter image description here

关于c# - 不使用 ASP.Net Core 2 中的迁移命令 Update-Database 创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48362092/

相关文章:

c# - 使用 DotNetOpenAuth 实现 API key

asp.net - [ValidateModel] 和 ASP.NET 中有效状态检查之间有什么区别?

c# - 多个客户端之间的对象同步算法

c# - 使用 XmlSerializer 添加不带前缀的命名空间

c# - 动态过滤linq lambda表达式

c# - 向现有 Web 服务自定义类型添加新字段

asp.net - 部署时自定义 JsonConverter 被忽略

docker - 在docker中构建net core 5.0时显示错误 "An error occurred while processing your request"(centos 8/VScode/Docker/EF Core)?

c# - 如何为 ASP.NET Core 应用程序配置 Azure 应用服务日志记录提供程序?

asp.net-mvc - Visual Studio 2017/2019 添加了缺失的区域