Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First foreign key without navigation property

Let's say I have the following entities:

public class Parent {     public int Id { get; set; } } public class Child {     public int Id { get; set; }     public int ParentId { get; set; } } 

What is the code first fluent API syntax to enforce that ParentId is created in the database with a foreign key constraint to the Parents table, without the need to have a navigation property?

I know that if I add a navigation property Parent to Child, then I can do this:

modelBuilder.Entity<Child>()     .HasRequired<Parent>(c => c.Parent)     .WithMany()     .HasForeignKey(c => c.ParentId); 

But I don't want the navigation property in this particular case.

like image 250
RationalGeek Avatar asked Jan 02 '14 15:01

RationalGeek


People also ask

How do I add a foreign key in code first?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table.

What is navigation property in Entity Framework Core?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name. (Required)

Does Entity Framework have a foreign key?

The Entity Framework Core Fluent API HasForeignKey method is used to specify which property is the foreign key in a relationship. In the following example, the AuthorFK property in the Book entity does not follow Entity Framework Core's convention for foreign key names.


1 Answers

Although this post is for Entity Framework not Entity Framework Core, It might be useful for someone who wants to achieve the same thing using Entity Framework Core (I am using V1.1.2).

I don't need navigation properties (although they're nice) because I am practicing DDD and I want Parent and Child to be two separate aggregate roots. I want them to be able to talk to each other via foreign key not through infrastructure-specific Entity Framework navigation properties.

All you have to do is to configure the relationship on one side using HasOne and WithMany without specifying the navigation properties (they're not there after all).

public class AppDbContext : DbContext {     public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) {}      protected override void OnModelCreating(ModelBuilder builder)     {         ......          builder.Entity<Parent>(b => {             b.HasKey(p => p.Id);             b.ToTable("Parent");         });          builder.Entity<Child>(b => {             b.HasKey(c => c.Id);             b.Property(c => c.ParentId).IsRequired();              // Without referencing navigation properties (they're not there anyway)             b.HasOne<Parent>()    // <---                 .WithMany()       // <---                 .HasForeignKey(c => c.ParentId);              // Just for comparison, with navigation properties defined,             // (let's say you call it Parent in the Child class and Children             // collection in Parent class), you might have to configure them              // like:             // b.HasOne(c => c.Parent)             //     .WithMany(p => p.Children)             //     .HasForeignKey(c => c.ParentId);              b.ToTable("Child");         });          ......     } } 

I am giving out examples on how to configure entity properties as well, but the most important one here is HasOne<>, WithMany() and HasForeignKey().

Hope it helps.

like image 147
David Liang Avatar answered Sep 21 '22 07:09

David Liang