2016-12-14 10 views
0

私はコアが.netのmvcアプリケーションを作っていますが、私は立ち往生しています。私のバックオフィスでは、GUIDの問題のため削除に問題があります。私の変数 'モデルは、' 問題を与える:暗黙のうちにBaseEntityに変換できません<System.Guid>

暗黙のうちに 'Overnight.Models.BaseEntity' 私のRoleController.csで

コードに型 'Overnight.Models.Security.ApplicationRole' を変換できません:

[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); 
     } 

これは私の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; } 
    } 
} 

は、これが私の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;}  
    } 
} 

私は私が代わりにTのGUIDを置くが、私は同じエラーに

EDIT持つ保つ異なるBaseEntities作ってみました:

をこれは私のActionBaseEntityViewModel.csです:

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

'ActionBaseEntityViewModel'の定義は何ですか? – Dai

+0

お返事ありがとうございます。 – hxwtch

+0

'IdentityRole 'の定義は何ですか? – Dai

答えて

0

問題はエラー状態と同じです。 ActionRoleViewModel.BaseEntityApplicationRoleに設定しようとしていますが、ApplicationRoleはBaseEntityから継承されないため、互換性がありません。

ActionRoleViewModel.BaseEntityプロパティのタイプをIBaseEntity<T>に変更し、ApplicationRoleにインターフェイスを追加することができます。これにより、必要な継承が作成されます。

だからあなたActionBaseEntityViewModelは次のようになります。

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

そして、あなたのApplicationRoleは次のようになります。

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

ありがとうございます、これは私のコードでエラーを修正しましたが、バックオフィスで役割を削除するとエラーが発生します: 'Overnight.Models.IBaseEntity'1 [System.Guid]'にデフォルトのコンストラクタがありません – hxwtch

+0

あなたの削除メソッドと署名に呼び出しコードを共有すると、おそらくもっと助けになるかもしれません。暗闇の中で刺すだけで、インターフェイスをEntity Frameworkに渡すことはできません。具体的なオブジェクトが必要です。繰り返しますが、私はここで仮定の多くを作ってるんだが、ApplicationRoleとしてあなたActionBaseEntityViewModel.BaseEntityを渡す/保存することはあなたの問題を解決することがあります。 'パブリッククラスActionBaseEntityViewModel :ActionViewModel { 公共ApplicationRole BaseEntity {取得します。セット; } } ' – Ketrex

関連する問題