脚本
我有一个进行数据库操作的方法(比方说).如果在该操作期间引发任何异常,我只想将该异常抛给调用者.我不想在catch块中执行任何特定任务,假设调用者将对该异常执行任何操作.在这种情况下,哪一种是适当的异常处理技术?
try
{
// Some work that may generate exception
}
catch(Exception)
{
throw;
}
finally
{
// Some final work
}
Run Code Online (Sandbox Code Playgroud)
以上是等于以下try/catch/finally吗?
try
{
// Some work that may generate exception
}
catch
{
throw;
}
finally
{
// Some final work
}
Run Code Online (Sandbox Code Playgroud)
以上是等于下面的try/finally吗?
try
{
// Some work that may generate exception
}
finally
{
// Some final work
}
Run Code Online (Sandbox Code Playgroud)
哪一个比另一个好?应该使用哪一个?
刚开始学习 .NET Core。我设法将Migration folder
,ApplicationDbContext
和ApplicationUser
文件移动到 .net 核心类库项目并将其引用到 Web 项目。这工作正常,因为我可以在我的数据库中看到与用户角色和声明相关的默认 7 个表。
现在我有类似的模型
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Domain.Model
{
[Table("Employee")]
public class Employee
{
[Key]
public int EmployeeId{get; set;}
[Required, MaxLength(100)]
public string Name {get;set;}
}
}
Run Code Online (Sandbox Code Playgroud)
在 ApplicationDbContext 文件中
namespace BLL
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
public DbSet<Employee> Employees {get;set;}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了控制器类,EmployeeController
并在 Index …
我的模型如下所示
public class Country
{
public int Id {get; set;}
public string Name {get; set;}
}
public class User
{
public int Id{get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
}
public class Company
{
public int Id{get; set;}
public string Name {get;set;}
public string City {get; set;}
public string Address1 {get; set;}
public string Address2 {get; set;}
[ForeignKey("Country")]
public int CountryId{get; set;}
public Country Country{get; set;}
public ICollection<CompanyUsers> CompanyUsers {get; …
Run Code Online (Sandbox Code Playgroud)