Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:30:16+00:00 2026-06-08T21:30:16+00:00

I am using code first and EF 4.1 im my MVC3 App and have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:30:18+00:00Added an answer on June 8, 2026 at 9:30 pm

    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.

    [ForeignKey("FarmID")]
    public virtual Farm Farm { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been using code first technique in ASP.NET MVC3 app. here it is
I am using ASP.NET MVC3 with EF Code First. I have not worked previously
I'm using Code First with EF 5. Currently I have SPs that return just
I have an MVC3 project I created using the Code First paradigm and it
I have a small MVC 3 app using Entity Framework Code First and use
I'm currently developing an app using ASP.NET MVC3 and Entity Framework 4.1 Code First
I am working on a mvc3.0 app using EF code first and mvc scaffolding.
I have an MVC3 Project running with EF Code First. Here is my code
I'm using Moq with a MVC3 project and EF (code first). I currently have
I developed a website using EF 4.1 code first,Mvc3 ,Sql Sever 2008 r2, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.