首页 > 解决方案 > 如果连接两个库,那么调试就不行了?

问题描述

我在“解决方案”中添加了两个类库。
我正在调试。
Chrome 启动。
在 Chrome 中,会出现起始页\Home\Index.cshtml
几秒钟后~10,调试自动停止。
 
描述。
我创建了一个项目 - WebApplCore;
我在解决方案中添加了一个类库 - Models;
我在解决方案中添加了一个类库 - DBRepository.

项目WebApplCore- ASP.NET 核心;
类库ModelsDBRepository-Net.Standard 2.0。

“测试”的条件符号:
- “+” -project包含在decision.
- “-” -project不包含在decision.

测试 1.
WebApplCore - "+";
型号 - “+”;
DBRepository - “-”。
结果:调试工作稳定。不会发生关闭调试。

测试 2.
WebApplCore - “+”。
楷模 - ”-”。DBRepository - “+”。
结果:调试工作稳定。不会发生关闭调试。

在此处输入图像描述

图片1
在此处输入图像描述

图二
在此处输入图像描述

图三
在此处输入图像描述

图四
在此处输入图像描述

图5
图5

标签: asp.netasp.net-core

解决方案


这是一个简单的工作演示,它通过添加类库的引用来显示 Web 应用项目中的用户列表:

1.Models类库

1)项目结构

在此处输入图像描述

2)用户模型

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }
    public string Gender { get; set; }
    public int  Age { get; set; }
}

2.DBRepository 类库

1)项目结构

在此处输入图像描述

2)DbContext,有关 EF Core 的更多详细信息,请参阅此处

public class MyDbContext:DbContext
{
    public MyDbContext (DbContextOptions<MyDbContext> options):base(options)
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if(!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=WebAppCore;Trusted_Connection=True;ConnectRetryCount=0");

        }
    }
    public DbSet<User> User { get; set; }
}

3.WebAPP核心

1)项目结构

在此处输入图像描述

2)索引视图

@model IEnumerable<Models.User>

<table class="table">
 <thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Gender)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th></th>
    </tr>
 </thead>
 <tbody>
    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
            <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
            <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            <a class="btn btn-success" data-toggle="modal" data-target="#exampleModal" data-accountidvalue="@item.Id">LinkDetails</a>
        </td>
    </tr>
    }
 </tbody>
</table>

3)家庭控制器

using DBRepository;

public class HomeController : Controller
{
    private readonly MyDbContext _context;

    public HomeController(MyDbContext context)
    {
        _context = context;
    }
    public IActionResult Index()
    {
        var model = _context.User.ToList();
        return View(model);
    }
 }

ConfigureServices4)Startup.cs,在方法中注册DbContext

public void ConfigureServices(IServiceCollection services)
    {
        var connection = @"Server=(localdb)\mssqllocaldb;Database=WebAppCore;Trusted_Connection=True;ConnectRetryCount=0";
        services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connection));

        services.AddControllersWithViews();
    }

5)在中使用以下命令Package Manager Console

PM> add-migration Initial  //create a migration
PM> update-database        //apply the migration to the database to create the schema

注意:DBRepository使用包管理器控制台的默认项目下拉列表 将您的目标项目更改为迁移项目( )。

这是我的演示链接,你可以参考。


推荐阅读