efcore icon indicating copy to clipboard operation
efcore copied to clipboard

EF7 Fluent API: Self Referencing Tables

Open WillBaldwin opened this issue 11 years ago • 4 comments

It appears as if EF7 uses all new Fluent API. Does anyone know where I might find more info? I'd like to implement self referencing tables with EF7.

public class Employee
{ public int EmployeeId //minus the gets and sets public Employee Manager //maps to the manager's Employee public int ManagerId //maps to the manager's Employee.EmployeeId public string Name public Employees }

//EF7 Fluent API does not seem to support the following.. perhaps there is a translation? HasOptional(x => x.Manager) //new Fluent API property maybe?? .WithMany(x => x.Employees) //new Fluent API .OneToMany .HasForeignKey(x => x.ManagerId) //new Fluent API ForeighKey .WillCascadeOnDelete(false); //new Fluent API property maybe??

WillBaldwin avatar Jan 24 '15 17:01 WillBaldwin

The EF 7 equivalent is .Entity() .HasOne(x => x.Manager) .WithMany(x => x.Employees) .ForeignKey(x => x.ManagerId) .Required(false);

Cascade on delete is not supported yet

AndriySvyryd avatar Jan 28 '15 01:01 AndriySvyryd

The following are not resolving for me, I assume they are introduced in later builds of EF7 after "7.0.0-beta1", is that correct?

.HasOne(x => x.Manager) .WithMany(x => x.Employees)

WillBaldwin avatar Jan 28 '15 15:01 WillBaldwin

Correct, they are currently in nightly builds. Here is some info on using the latest builds https://github.com/aspnet/EntityFramework/wiki/Getting-Started-with-Nightly-Builds.

rowanmiller avatar Jan 28 '15 17:01 rowanmiller

@rowanmiller @AndriySvyryd Alright, no problems with storing the data, but is there a way to retrieve a hierarchy of depth n? #3241

weitzhandler avatar Sep 26 '15 21:09 weitzhandler