Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF One-to-many Foreign Keys without child navigation properties

Using code-first Entity Framework and .NET 4, I'm trying to create a one-to-many relationship between parents to children:

public class Parent
{
    [Key]
    public int ParentId { get; set; }
    [Required]
    public string ParentName { get; set; }
    public IEnumerable<Child> Children { get; set; }
}

public class Child
{
    [Key]
    public int ChildId { get;  set; }
    [ForeignKey]
    public int ParentId { get; set; }
    [Required]
    public string ChildName { get; set; }
}

As pointed out here, in order for foreign key relationship to carry into the database, the actual objects must be linked, not just their IDs. The normal way to do this if for a child to contain a reference to its parent (example).

But how do I enforce foreign keys in my implementation, which is the other way around (parent referencing children)?

like image 532
Arithmomaniac Avatar asked Jul 05 '12 18:07

Arithmomaniac


People also ask

Where does the foreign key go in one-to-many relationship?

If there is a one-to-many relationship between two entities, add the key of the entity on the “one” side (the parent) into the child table as a foreign key.

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.


1 Answers

First of all: You cannot use IEnumerable<T> for a collection navigation property. EF will just ignore this property. Use ICollection<T> instead.

When you have changed this, in your particular example you don't need to do anything because the foreign key property name follows the convention (name of primary key ParentId in principal entity Parent) so that EF will detect a required one-to-many relationship between Parent and Child automatically.

If you had another "unconventional" FK property name you still could define such a mapping with Fluent API, for example:

public class Child
{
    [Key]
    public int ChildId { get;  set; }

    public int SomeOtherId { get; set; }

    [Required]
    public string ChildName { get; set; }
}

Mapping:

modelBuilder.Entity<Parent>()
    .HasMany(p => p.Children)
    .WithRequired()
    .HasForeignKey(c => c.SomeOtherId);

As far as I can tell it is not possible to define this relationship with data annotations. Usage of the [ForeignKey] attribute requires a navigation property in the dependent entity where the foreign key property is in.

like image 108
Slauma Avatar answered Nov 09 '22 06:11

Slauma