为什么我的网页在添加数据库后突然无法使用了?

问题描述 投票:0回答:1

我有一个 ASP.NET Core 6.0 MVC Web 应用程序项目、Entity Framework Core 7.0.9 和 Identity 6.0.2。

在将数据库添加到项目之前,所有页面都已在浏览器中查找并显示。但是在添加数据库后,当我想打开页面时出现此错误:

处理请求时发生未处理的异常。 MissingMethodException:找不到方法:'布尔值 Microsoft.AspNetCore.Cryptography.UnsafeNativeMethods.CryptProtectData(Microsoft.AspNetCore.Cryptography.DATA_BLOB*, IntPtr,Microsoft.AspNetCore.Cryptography.DATA_BLOB*,IntPtr,IntPtr, UInt32,Microsoft.AspNetCore.Cryptography.DATA_BLOB ByRef)'。

Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.ProtectWithDpapiCore(字节* pbSecret,uint cbSecret,字节* pbOptionalEntropy,uint cbOptionalEntropy, bool fLocalMachine) CryptographicException: An 尝试加密提供的数据时发生错误。请参阅 内部异常以获取更多信息。欲了解更多信息,请访问 http://aka.ms/dataprotectionwarning

Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(byte[] 明文)

即使页面及其控制器尚未附加到数据库,也会显示此错误。

我现在只编写角色表,这是控制器:

public class ManageRoleController : Controller
{
        private readonly RoleManager<IdentityRole> _roleManager;

        public ManageRoleController(RoleManager<IdentityRole> roleManager)
        {
            _roleManager = roleManager;
        }

        public IActionResult Index()
        {
            var roles = _roleManager.Roles.ToList();
            return View("Role", roles);
        }
}

但是当我想打开这个页面时,页面是完全白色的,不显示任何内容。

如何解决这两个问题?

这是我的角色页面 html :

<div class=" mt-5   mx-2" id="mainContent">
    <div class="row  text-end mt-0 mb-5  border-0 border-bottom border-bottom-2 border-dark  ">
        <div class="col-12 mb-2 "><b>مدیریت نقش‌ها</b></div>
        <div class="col-12">
            <table class="table table-light table-striped  border border-1  table-hover text-center w-100" id="ReoprtTable">
                <thead>
                    <tr>

                        <th  style="width:90px;">ردیف  </th>
                        <th  >نقش</th>
                        <th  >عملیات </th>
                    </tr>
                </thead>
                <tbody>

                    @{
                        if (!Model.Any())
                        {
                            <tr>
                                <td colspan="3">هنوز هیچ مقامی تعریف نشده است</td>
                            </tr>
                        }
                        else
                        {
                            for (int i = 1; i < Model.Count + 1; i++)
                            {
                                <tr>
                                    <td style="display:none;">@Model[i - 1].Id</td>
                                    <td>@i</td>
                                    <td>@Model[i - 1].Name</td>
                                    <td>
                                        <a data-toggle="modal" href="#exampleModal" class="btn btn-warning testeditbtn w-50  float-right Update_Button">بروزرسانی</a>

                                        <button type="button" class="btn btn-danger float-left w-50 Delete_Button ">حذف</button>
                                    </td>
                                </tr>
                            }
                        }
                    }
                </tbody>
            </table>

            <div class="col-12 my-2">
                <form asp-action="Search" asp-controller="User" class="d-flex w-50  " method="post">

                    <input class="form-control   border-info " type="search" placeholder="نقش جدید" aria-label="نقش جدید" name="search">
                    <button class="btn btn-outline-info  bg-info text-light me-2" type="submit">افزودن</button>
                </form>
            </div>
        </div>
    </div>
</div>

这是数据库类:(我不是复制数据库集)

public class DB : IdentityDbContext<User>
{
        public DB() : base() { }

        public DB(DbContextOptions<DB> options) : base(options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder)
        {
            OptionsBuilder.UseSqlServer(@"data source=.;initial catalog = stor; integrated security = true;TrustServerCertificate=True;");
            base.OnConfiguring(OptionsBuilder);
        }
}

请帮忙。

asp.net-core-mvc asp.net-core-identity asp.net-core-6.0
1个回答
0
投票

错误消息表明 nuget 包不兼容问题。

但是,

Microsoft.EntityFrameworkCore --version 7.0.9
可以在 .net 6 应用程序中与
Microsoft.AspNetCore.Identity.EntityFrameworkCore --version 6.0.0
一起使用。请参阅此官方文档

EF7 面向 .NET 6,因此可以与 .NET 6 (LTS) 或 .NET 7.

OP还好心地证明了还有另一个nuget包与身份版本和.net 6不兼容。将它们全部升级到最新版本并使用.NET 7可以解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.