私は以下のコードを持っています。それは時々ランタイムエラーを与える:SaveOrUpdateの代わりにMergeを使用
a different object with the same identifier value was already associated with the session
私はMerge
代わりのSaveOrUpdate
を使用することを提案しているソリューションを見ました。それを試してみると(コメントアウトされた行を参照)。
The type T must be a reference type in order to use it as a parameter
T
in the generic type or method
このエラーの解決策は、クラス宣言にT : class
を追加すると言う:私は、コンパイルエラーを取得します。すでにT : new()
です。私はそれを "クラス"に変更すると、私は他のコンパイルエラーを取得します。
'T' must be an abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
とも私はこれを行うにはどうすればよいGetDefaultInstance()
を参照してください?
public class GenericNHibernateDataService<T, ID> : Interface.Data.IGenericDataService<T, ID> where T : new()
public virtual T GetDefaultInstance()
{
return new T(); // compile error when changing to T : class
// Cannot create an instance of the variable type 'T' because it does not have the new constraint
}
public virtual T SaveOrUpdate (T entity)
{
NHibernate.ITransaction tx = null;
try
{
tx = this.Session.BeginTransaction();
this.Session.SaveOrUpdate (entity);
//Session.Merge(entity); //The type T must be a reference type in order to use it as a parameter `T` in the generic type or method
tx.Commit();
}
catch (System.Exception ex)
{
tx.Rollback();
throw ex;
}
return entity;
}
...
}
あなたは両方の制約を追加しようとしたことがありますか? –
それは動作します!あなたが好きなら答えとして投稿してください。 –
完了。うれしかった。 –