I am using code first and EF 4.1 im my MVC3 App and have two models that have two
relationships between them. Here is the classes that refers to these models:
public class Cattle
{
public int CattleID { get; set; }
public int FarmID { get; set; }
public int SituationID { get; set; }
public int DiscardID { get; set; }
public int Stamp { get; set; }
public string Sex { get; set; }
public string Condition { get; set; }
public virtual Farm Farm { get; set; }
[InverseProperty("Cattles2")]
public virtual ICollection<Farm> Farms { get; set; }
}
public class Farm
{
public int FarmID { get; set; }
public string Name { get; set; }
public virtual ICollection<Cattle> Cattles { get; set; }
public virtual ICollection<Cattle> Cattles2 { get; set; }
}
One relationship is one to one or many cause a catle can only be in a farm and a farm can contains many catles. Another relationship is many to many that a catle can be transferred between farms and i would generate a third table to store the transfers using Fluent API. I also would like to insert a Date property to the new table and don´t know how to do it.Here is the code in the FarmContext inside the OnModelCreating method:
modelBuilder.Entity<Catle>()
.HasMany(c => c.Farms).WithMany(i => i.Catles)
.Map(t => t.MapLeftKey("CatleID")
.MapRightKey("FarmID")
.ToTable("CatleTransfer"));
When i build the project it seems to work fine and the generated tables Catle and Farm are fed by the FarmInitializer but some information that points to this relationship is missing in the index page of Catle. its appearing in blank to me. Here is the code that gets the information:
@Html.DisplayFor(modelItem => item.Farm.Name)
I need Knowing what i am doing wrong or if there is a more appropriate method to resolve that issue.
You may need to be specific about the relationship when dealing with multiple joins.
Try adding a Foreign Key attribute to your Farm navigation property.