Cannot implicitly convert to BaseEntity<System.Guid>

865 Views Asked by At

I'm making an mvc app with .net core and I'm stuck. In my backoffice there is a problem with my delete because of a guid issue. My variable 'model' gives the issue:

Cannot implicitly convert type 'Overnight.Models.Security.ApplicationRole' to 'Overnight.Models.BaseEntity'

the code in my 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);
        }

this is my BaseEntity.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; }
    }
}

This is my ActionRoleViewModel.cs :

namespace Overnight.Models.ViewModels
{
    public class ActionRoleViewModel : ActionBaseEntityViewModel<Guid>
    {
    }
}

This is my 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;}      
    }
}

I tried making different BaseEntities where I put Guid instead of T but I keep having the same error

EDIT:

this is my ActionBaseEntityViewModel.cs :

namespace Overnight.Models.ViewModels
{
    public class ActionBaseEntityViewModel<T>: ActionViewModel
    {
        public BaseEntity<T> BaseEntity { get; set; }
    }
}
1

There are 1 best solutions below

2
On

The problem is just as the error states. You are trying to set ActionRoleViewModel.BaseEntity to ApplicationRole, but ApplicationRole does not inherit from BaseEntity, so they are incompatible.

You could modify your ActionRoleViewModel.BaseEntity property to be of type IBaseEntity<T>, and add the interface to your ApplicationRole. This would create the necessary inheritance.

So your ActionBaseEntityViewModel would look like this:

public class ActionBaseEntityViewModel<T>: ActionViewModel
{
    public IBaseEntity<T> BaseEntity { get; set; }
}

And your ApplicationRole would look like this:

public class ApplicationRole : IdentityRole<Guid>, IBaseEntity<Guid>
{
    public DateTime CreatedAt {get; set;}
    public Nullable<DateTime> UpdatedAt {get; set;}
    public Nullable<DateTime> DeletedAt {get; set;}      
}