我正在用.net core制作一个MVC应用程序,我被卡住了。在我的后退中,由于GUID问题,我的删除存在问题。我的变量"模型"给出了问题:
不能隐式将类型'overnight.models.security.applicationrole'转换为" overnight.models.baseentity"
我的rolecontroller.cs中的代码:
[HttpGet("[area]/[controller]/[action]/{id:Guid}")]
public async Task<IActionResult> Delete(Guid id, [FromQuery] ActionType actionType)
{
var model = await ApplicationDbContext.Roles.FirstOrDefaultAsync(m => m.Id == id);
if(model == null)
{
return RedirectToAction("Index");
}
var viewModel = new ActionRoleViewModel()
{
//error: Cannot implicitly convert type 'Overnight.Models.Security.ApplicationRole' to 'Overnight.Models.BaseEntity<System.Guid>'
BaseEntity = model,
ActionType = actionType
};
return View(viewModel);
}
这是我的基础。cs:
namespace Overnight.Models
{
public class BaseEntity<T> : IBaseEntity<T>
{
public T Id { get; set; }
public DateTime CreatedAt { get; set; }
public Nullable<DateTime> UpdatedAt { get; set; }
public Nullable<DateTime> DeletedAt { get; set; }
}
}
这是我的ActionRoleviewModel.cs:
namespace Overnight.Models.ViewModels
{
public class ActionRoleViewModel : ActionBaseEntityViewModel<Guid>
{
}
}
这是我的applicationRole.cs:
namespace Overnight.Models.Security
{
public class ApplicationRole : IdentityRole<Guid>
{
public DateTime CreatedAt {get; set;}
public Nullable<DateTime> UpdatedAt {get; set;}
public Nullable<DateTime> DeletedAt {get; set;}
}
}
我尝试制作不同的基础,而不是将GUID而不是T放置,但是我一直存在相同的错误
编辑:
这是我的ActionBaseEntityViewModel.cs:
namespace Overnight.Models.ViewModels
{
public class ActionBaseEntityViewModel<T>: ActionViewModel
{
public BaseEntity<T> BaseEntity { get; set; }
}
}
问题与错误所述一样。您正在尝试将ActionRoleViewModel.BaseEntity
设置为ApplicationRole
,但是ApplicationRole
不会从基数继承,因此它们不兼容。
您可以将ActionRoleViewModel.BaseEntity
属性修改为类型IBaseEntity<T>
,然后将接口添加到ApplicationRole
。这将创建必要的继承。
,您的ActionBaseEntityViewModel
看起来像这样:
public class ActionBaseEntityViewModel<T>: ActionViewModel
{
public IBaseEntity<T> BaseEntity { get; set; }
}
您的应用程序看起来像这样:
public class ApplicationRole : IdentityRole<Guid>, IBaseEntity<Guid>
{
public DateTime CreatedAt {get; set;}
public Nullable<DateTime> UpdatedAt {get; set;}
public Nullable<DateTime> DeletedAt {get; set;}
}