c# - EF Code First Migration modelBuilder -


i have 2 entities in c# code:

  public class currency {             public int currencyid { get; set; }      public string currencyname { get; set; }      public virtual icollection<currencyrate> currentcurrencyrates { get; set; }     public virtual icollection<currencyrate> targetcurrencyrates { get; set; } }   public class currencyrate     {         public int rateid { get; set; }         public datetime ratedate { get; set; }         public decimal ratevalue { get; set; }         public int currentcurrencyid { get; set; }         public int targetcurrencyid { get; set; }           public virtual currency currentcurrency { get; set; }         public virtual currency targetcurrency { get; set; }     } 

and have context class:

public economicappcontext()             : base("economicapp")         { }          protected override void onmodelcreating(dbmodelbuilder modelbuilder)         {             modelbuilder.entity<currencyrate>().property(v => v.ratevalue).hascolumntype("money");             modelbuilder.entity<currencyrate>().haskey(cr => cr.rateid);             modelbuilder.entity<currency>().haskey(c => c.currencyid);              modelbuilder.entity<currency>().                 hasmany<currencyrate>(c => c.currentcurrencyrates).                 withrequired(c => c.currentcurrency).                 hasforeignkey(c => c.rateid);             modelbuilder.entity<currency>().                hasmany<currencyrate>(c => c.targetcurrencyrates).                withrequired(c => c.targetcurrency).                hasforeignkey(c => c.rateid);               base.onmodelcreating(modelbuilder);         }          public dbset<currencyrate> currencyrates { get; set; }         public dbset<currency> currencies { get; set; } 

i'm trying migration existing db. wanna schema :

enter image description here

and i'm doing wrong? may me migrate tables db?

try following:

modelbuilder.entity<currencyrate>().                 hasrequired<currency>(c => c.currentcurrency).                 withmany(c => c.currentcurrencyrates).                 hasforeignkey(c => c.currentcurrencyid); modelbuilder.entity<currencyrate>().                hasrequired<currency>(c => c.targetcurrency).                withmany(c => c.targetcurrencyrates).                hasforeignkey(c => c.targetcurrencyid); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -