sql - EF6 code first - Identity is unique combinaison of two column in table -


good morning,

i using ef6 code first , try achieve following :

i have class/table fighter:

public class fighter {     [key]     [databasegeneratedattribute(databasegeneratedoption.identity)]     public int fighterid { get; set; }     public string name { get; set; } } 

i want keep track of different number of wins between fighters created other class: fightresult

public class fightresult {     public virtual fighter fighter1 { get; set; }     public virtual fighter fighter2{ get; set; }     public uint wins1 { get; set; }     public uint wins2 { get; set; } } 

i combinaisons of fighter1 , fighter2 unique. exemple let's entry exists:

+----------+----------+-------+-------+ | fighter1 | fighter2 | wins1 | wins2 | +----------+----------+-------+-------+ | jon cena | yoda     |     0 |     0 | +----------+----------+-------+-------+ 

i don't want able enter entry same fighters example these two:

+----------+----------+-------+-------+ | fighter1 | fighter2 | wins1 | wins2 | +----------+----------+-------+-------+ | jon cena | yoda     |     0 |     1 | | yoda     | jon cena |     0 |     0 | +----------+----------+-------+-------+ 

there maybe better way handle entity framework, if have suggestion happy read it.

thanks

you can try write (it create composite primary key: fighter1id fighter2id, advice set keys int type instead of string specify on picture, if fighter class has int fighterid shown above).

    public class fightresult {          [key, column(order = 0)]                 public int fighter1id { get; set; }                 [key, column(order = 1)]                 public int fighter2id { get; set; }                  public virtual fighter fighter1 { get; set; }         public virtual fighter fighter2{ get; set; }         public uint wins1 { get; set; }         public uint wins2 { get; set; }   } 

then before add new items can sort fighters id, example:

//you have 2 fighters: fighter1 , fighter2 var firstfighter = fighter1.id > fighter2.id ? fighter1 : fighter2; var secondfighter = fighter1.id > fighter2.id ? fighter2 : fighter1; db.fightresult.add(new fightresult { fighter1 = firstfighter, fighter2 = secondfighter, wins1 = 1, wins2 = 0}); 

also can try approach (i recommend first one, because think may pitfals second variant):

    public class fightresult {             [key, databasegenerated(databasegeneratedoption.none)]                public int id {get; set;}          public virtual fighter fighter1 { get; set; }         public virtual fighter fighter2 { get; set; }         public uint wins1 { get; set; }         public uint wins2 { get; set; }          public fightresult(fighter fighter1, fighter fighter2, uint wins1, uint wins2)         {             //1. assign constructor arguments corresponding instance fields             //2. generate uniq id based on pair of fighters (independently on order)             id = _generateid(fighter1, fighter2);         }  } 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -