0

I am still starting with Entity Framework Code First. I want to be able to select a Resource from list when creating a new resource. How do I reference a Resource with a Resource model.

public class Resource
{

    public int ResourceId { get; set; }
    [Required]
    [DataType(DataType.EmailAddress)]
    [EmailAddress]
    public string EmailAddress { get; set; }
    [Required]
    public string Password { get; set; }
    public string FullName { get; set; }


    public int TimeManagerId { get; set; }

    public int TravelManagerId { get; set; }

    public int OvertimeManagerId { get; set; }

    public int AbsenceManagerId { get; set; }
    public virtual Resource TimeManager { get; set; }
    public virtual Resource TravelManager { get; set; }
    public virtual Resource OvertimeManager { get; set; }
    public virtual Resource AbsenceManager { get; set; }

}
4
  • This looks good. What's wrong with this? Do you get an error? What exactly is your question? Commented Feb 18, 2014 at 6:58
  • When I generate the controller, shouldn't the fields be dropdownlists? I currently just display int textfields Commented Feb 18, 2014 at 7:00
  • I don't understand what is logical model of this. But did you try anything? Commented Feb 18, 2014 at 7:00
  • @razeth01 this depends on your requirements. You current model says that every Resource has a TimeManager, a TravelManager etc. If you need to have a list of other resources, then you need to add a collection of resources: public virtual ICollection<Resource> MyResources { get; set; } Commented Feb 18, 2014 at 7:03

1 Answer 1

2

I think you're pretty close! If you want to do this by convention, you can change the foreign keys in your model to the form of [navigation property name][principal primary key property name]. Specifically, change Id to ResourceId so it matches the primary of the table you're referencing (which happens to be itself)...

public int TimeManagerResourceId { get; set; }
public int TravelManagerResourceId { get; set; }
public int OvertimeManagerResourceId { get; set; }
public int AbsenceManagerResourceId { get; set; }

Since you're just starting with EF code first, I'd recommend you install the Entity Framework Power Tools. You'll be able to right-click on the .cs file containing your DbContext, and it'll generate a read-only diagram of the mappings.

Try it out with your current model... right-click on entity in the diagram and view Table Mappings. You'll see EF wasn't able to figure out your foreign keys and created 4 more for you. Once you make the changes above, generate the diagram again and see the difference.

Edit: Docs on code first conventions... http://msdn.microsoft.com/en-us/data/jj679962.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Just updated the post with a reference for code first conventions if you are interested. Also, these are often referred to as "self-referencing" or "hierarchical" entities... that's handy if you need to search for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.