2011-10-09 77 views
0

我有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; }  
} 

回答

1

只需使用你原來的IEntityKey接口,配有TupleTKey

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不是一個實體,而只是一個關聯...

+0

哦,我的話......答案很簡單......謝謝托馬斯 – Eminem

相關問題