[英]Incorrect number of type parameters in reference class MyClass<F,T> - Generic entity framework
I've got the following generic abstract class: 我有以下通用抽象类:
public abstract class MyClass<F, T>
where TCurrencyFrom : Book
where TCurrencyTo : Book
{
public int Id { get; set; }
public virtual F First{ get; set; }
public virtual T Second { get; set; }
}
And I got 3 classes which implement this class like: 我有3个实现此类的类,例如:
public class Implementation1 : MyClass<BookType1, BookType2>
{
}
public class Implementation2 : MyClass<BookType2, BookType1>
{
}
Now I got an "EntityTypeConfiguration" for those which looks like: 现在,我得到了一个类似于以下内容的“ EntityTypeConfiguration”:
public class MyClassConfiguration<TMyClass> : EntityTypeConfiguration<TMyClass> where TMyClass: MyClass
{
public MyClassConfiguration()
{
...
}
}
And try to use those like: 并尝试使用类似的方式:
public class Implementation1Map : MyClassConfiguration<Implementation1>
{
public Implementation1Map ()
{
...
}
}
But then I get the following error: 但是然后我得到以下错误:
Incorrect number of type parameters in reference class MyClass
参考类MyClass中类型参数的数量不正确
How can I solve this problem and make sure I have a generic approach on the EntityTypeConfigurations? 如何解决此问题,并确保对EntityTypeConfigurations具有通用方法?
Unfortunately this is tricky with .NET generics. 不幸的是,这对于.NET泛型来说是棘手的。
If MyClassConfiguration
doesn't actually care about the type arguments, you might want to create a non-generic interface: 如果
MyClassConfiguration
实际上并不关心类型参数,则可能要创建一个非通用接口:
public interface IMyClass
{
// Any members of MyClass<,> which don't rely on the type arguments,
// e.g. the Id property
}
Then make MyClass<,>
implement IMyClass
: 然后使
MyClass<,>
实现IMyClass
:
// Type parameters renamed to make the type constraints sensible...
public abstract class MyClass<TCurrencyFrom, TCurrencyTo> : IMyClass
where TCurrencyFrom : Book
where TCurrencyTo : Book
And change the type constraint for MyClassConfiguration
: 并更改
MyClassConfiguration
的类型约束:
public class MyClassConfiguration<TMyClass> : EntityTypeConfiguration<TMyClass>
where TMyClass: IMyClass
(Obviously you'll want to give IMyClass
a more useful name...) (显然,您要给
IMyClass
一个更有用的名称...)
Alternatively, just make MyClassConfiguration
generic in three type parameters: 或者,只需在三个类型参数中使
MyClassConfiguration
通用:
public class MyClassConfiguration<TMyClass, TCurrencyFrom, TCurrencyTo>
: EntityTypeConfiguration<TMyClass>
where TMyClass: MyClass<TCurrencyFrom, TCurrencyTo>
where TCurrencyFrom : Book
where TCurrencyTo : Book
public class Implementation1Map
: MyClassConfiguration<Implementation1, BookType1, BookType2>
public class Implementation2Map
: MyClassConfiguration<Implementation2, BookType2, BookType1>
It's ugly, but it'll work. 很难看,但是可以用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.