2016-09-17 7 views
0

は、ApplicationUser基本クラスから継承された学生のクラス(ASP.NET同一性)があり、以下に示すように、それはStudentViewModelと呼ばのViewModelがある:なぜAutomapperベース・継承クラスのために働いていないMVCアプリケーションで

エンティティークラス:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, 
            ApplicationUserRole, ApplicationUserClaim>, IUser<int> 
{ 
    public string Name { get; set; } 
    public string Surname { get; set; } 
    //code omitted for brevity 
} 

public class Student: ApplicationUser 
{  
    public int? Number { get; set; } 
} 

のViewModel:

public class StudentViewModel 
{ 
    public int Id { get; set; }  
    public int? Number { get; set; } 
    //code omitted for brevity 
} 

私はコントローラでApplicationUserへのマッピングStudentViewModelによって学生を更新するために、次のメソッドを使用します。

[HttpPost] 
[ValidateAntiForgeryToken] 
public JsonResult Update([Bind(Exclude = null)] StudentViewModel model) 
{ 
    //Mapping StudentViewModel to ApplicationUser :::::::::::::::: 
    var student = (Object)null; 

    Mapper.Initialize(cfg => 
    { 
     cfg.CreateMap<StudentViewModel, Student>() 
      .ForMember(dest => dest.Id, opt => opt.Ignore()) 
      .ForAllOtherMembers(opts => opts.Ignore()); 
    }); 

    Mapper.AssertConfigurationIsValid(); 
    student = Mapper.Map<Student>(model); 
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

    //Then I want to pass the mapped property to the UserManager's Update method: 
    var result = UserManager.Update(student); 

    //code omitted for brevity    
} 

この方法を使用するとき、私はエラーが発生します。

The type arguments for method 'UserManagerExtensions.Update(UserManager, TUser)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

任意のアイデアへ修理する?

+0

@BalagurunathanMarimuthuにそれを変更するかを持っていますか? –

答えて

1

エラーは、AutoMapperとは関係ありません。

問題は、それがStudentをする必要がありますしながら、あなたのstudent変数が原因次の行

var student = (Object)null; 

にタイプobjectであるということです。あなたはどんな考えを

上の行を削除し、

var student = Mapper.Map<Student>(model); 

を使用するか、

Student student = null; 
+0

ご返信ありがとうございました。 ** student student = null; **を使用しようとしましたが、その場合、student = Mapper.Map (model); ** lineの後に学生プロパティがnullになりました。間違いはありますか?一方、継承を使用すると、Automapperでベース/継承クラスのマッピングを使用する別の解決策がありますか? –

+0

'Mapper.Map'の結果は、受信側の変数の型には関係ありません。コードがコンパイルされると、マッピングの問題があるようです。私は '.ForAllOtherMembers(opts => opts.Ignore())'の呼び出しをチェックします。あなたが 'StudentViewModel'のすべてのメンバーを無視している(マッピングしていない)と聞こえたら、その呼び出しを削除することを検討してください。 –

+0

はい、そうです。私は、「マップされていないメンバーが見つかりました」に示されている関連プロパティを無視しました。エラー。ただし、生徒の変数には新しいデータが正しく入力されていますが、エラーがなくても** UserManager.Update(student)**は生徒を更新できません。私はApplicationUserから継承したので、Studentクラスの** ApplicationUser ** instedも使用しようとしましたが、それは意味をなさないので、レコードは更新されません。何か案が? –

関連する問題