ledgeJumper
ledgeJumper

Reputation: 3630

Setting up Foreign key like references in EF code first

I have a set of four POCO objects that need to reference themselves in this way:

A Freelancer can have multiple Clients

Clients can have multiple Projects

Projects can have multiple Stories.

One this I want to make sure is possible is that a freelancer starts off with no clients, and client starts off with no projects, and a project starts off with no stories, so I guess they need to be nullable?

The opposite is true the other direction, a story NEEDS a project, project needs a client, and a client needs a freelancer.

I just want to see if there is anything that I need to do when I am creating the model (onModelCreating override) to make sure this is the relationship that happens.

Here are my objects:

public class Freelancer
{
    public int ID { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string CompanyName { get; set; }
    public string Avatar { get; set; }
    public Address FreelancerAddress { get; set; }
    public ICollection<Client> Clients { get; set; }
}


public class Client
{
    public int ID { get; set; }
    public string Name { get; set; }
    public Address ClientAddress { get; set; }
    public string Logo { get; set; }
    public ICollection<Project> Projects { get; set; }
}


public class Project
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Summary { get; set; }
    public string Description { get; set; }
    public ICollection<Story> Stories { get; set; }
}


 public class Story
{
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public decimal Duration { get; set; }
    public bool Billable { get; set; }
    public string Notes { get; set; }
}

I understand EF does some things automatically, I am just asking if there is more I need to do to make sure I have the relationship I want. Thanks

Upvotes: 1

Views: 125

Answers (1)

Slauma
Slauma

Reputation: 177133

By convention your model will create optional relationships ("client can have a freelancer but doesn't need to") but since you want required relationships ("client needs a freelancer") you must define it with Fluent API:

modelBuilder.Entity<Freelancer>()
    .HasMany(f => f.Clients)
    .WithRequired()
    .Map(m => m.MapKey("FreelancerID"));  // FK column name in Clients table

You can work without the last line (Map). EF will then create a default foreign key name, something with underscore, maybe Freelancer_ID.

And the same mapping for the other relationships.

Alternatively you can introduce inverse navigation properties with foreign key properties:

public class Client
{
    public int ID { get; set; }
    //...
    public ICollection<Project> Projects { get; set; }

    public int FreelancerID { get; set; }
    public Freelancer Freelancer { get; set; }
}

With such a model EF will recognize the relationships as required automatically because the foreign key property FreelancerID is not nullable and you don't need additional mapping with Fluent API.

Upvotes: 1

Related Questions