asp.net - 如果连接两个库,那么调试就不行了?
问题描述
我在“解决方案”中添加了两个类库。
我正在调试。
Chrome 启动。
在 Chrome 中,会出现起始页\Home\Index.cshtml
。
几秒钟后~10
,调试自动停止。
描述。
我创建了一个项目 - WebApplCore
;
我在解决方案中添加了一个类库 - Models
;
我在解决方案中添加了一个类库 - DBRepository
.
项目WebApplCore
- ASP.NET 核心;
类库Models
,DBRepository
-Net.Standard 2.0。
“测试”的条件符号:
- “+” -project
包含在decision
.
- “-” -project
不包含在decision
.
测试 1.
WebApplCore - "+";
型号 - “+”;
DBRepository - “-”。
结果:调试工作稳定。不会发生关闭调试。
测试 2.
WebApplCore - “+”。
楷模 - ”-”。DBRepository - “+”。
结果:调试工作稳定。不会发生关闭调试。
解决方案
这是一个简单的工作演示,它通过添加类库的引用来显示 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);
}
}
ConfigureServices
4)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
使用包管理器控制台的默认项目下拉列表 将您的目标项目更改为迁移项目( )。
这是我的演示链接,你可以参考。
推荐阅读
- c# - 获取添加到代理方法的 InterfaceInterceptorBehaviour 列表以及如何确认所有行为的 Invoke 方法在 c# 中执行
- angular - 如果存在令牌,如何将应用程序的启动重定向到主组件
- pgadmin-4 - pgAdmin - 截断 PID 详细信息下显示的 SQL
- wordpress-plugin-creation - 有没有办法向 WordPress 管理子菜单项添加一个类(没有 JavaScript)
- pandas - 无法从 pandas._typing 导入名称标签
- forms - 使用vue在element-ui中验证错误后如何向上滚动到第一个错误元素?
- configuration - TYPO3 10 LTS:使 .scss 文件在后端可编辑,就像 .css 文件一样
- javascript - Windows 10 上 Chrome 中的网络蓝牙连接问题
- r - 如何从(纯)R中的语法生成序列?
- python - os.system重定向python代码不起作用