我有3张桌子:
Player => PlayerId (Primary Key), Name, etc
Team => TeamId (Primary Key), Name, etc
PlayerTeam => PlayerId, TeamId (The combination must be unique)
然后我有以下内容:
public interface IEntityKey<TKey>
{
TKey Id { get; }
}
public interface IRepository<TKey, TEntity> where TEntity : class, IEntityKey<TKey>
{
IQueryable<TEntity> All();
TEntity FindBy(Expression<Func<TEntity, bool>> expression);
....
TEntity FindBy(TKey id);
}
现在,虽然上述内容适用于具有单个键的表(例如播放器或团队表),但我如何为我的playerteam表实现它?我需要的是一个满足任意数量的键的解决方案。我更喜欢像......这样的解决方案。
public interface IEntityKey<TKey> where TKey : System.Tuple
{
TKey Id { get; }
}
结束
public interface IEntityKey<TKey1, TKey2>
{
TKey1 Id1 { get; }
TKey2 Id2 { get; }
}
答案 0 :(得分:1)
只需使用原始IEntityKey
界面,Tuple
为TKey
:
public class PlayerTeam : IEntityKey<Tuple<int, int>>
{
Tuple<int, int> IEntityKey<Tuple<int, int>>.Id
{
get { return Tuple.Create(PlayerId, TeamId); }
}
public int PlayerId { get; set; }
public int TeamId { get; set; }
}
作为旁注,在我看来,PlayerTeam
不是一个实体,而只是一个联想......