Entity framework code first, custom foreign key name for inheritance

Cédric Bourgeois

I have a class named MyEntity

public class MyEntity
{
    public int Id { get; set; }

    [Required]
    [StringLength(300)]
    public string Name { get; set; }
}

And an inherited class HubEntity

public class HubEntity : Entity
{
  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.None)]
  public int EntityId { get; set; }

  [Required]
  [StringLength(300)]
  public string RootName { get; set; }
}

This is based on existing tables in the database. There is one table for each class. Thus the EntityId field in the HubEntity table refers to the Id field in the Entity table.

DB Model

In order to avoid demolishing the existing database, I run my code on a empty database and let the EF create the tables:

Actual DB model

My concern is that the HubEntity table is built with both the Id and EntityId fields, the ID field being designed as the foreign key to the Entity table. I don't know how to get the EntityId field to be used as the foreign key.

I tried to tag it [ForeignKey("Entity")], but then I get an exception: the program looks for an Entity property in HubEntity that points to an Entity, which is not what I want because it's heritage.

I first created the application using DB first and here is what the model was looking like: DB Firts model

Thanks in advance for your help.

Cedric

Cédric Bourgeois

OK, got it.

The class MyEntity is ok.

public class MyEntity
{
    public int Id { get; set; }

    [Required]
    [StringLength(300)]
    public string Name { get; set; }
}

The inherited class HubEntity must not contain the EntityId field:

public class HubEntity : Entity
{
  [Required]
  [StringLength(300)]
  public string RootName { get; set; }
}

Now, in the MyModel.cs, in OnModelCreating, add the following clause:

  modelBuilder.Entity<HubEntity>()
    .Property(e => e.Id)
    .HasColumnName("EntityId");

And the foreign key will be generated with the desired name. :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework: Foreign Key in code first

From Dev

Entity Framework Inheritance Code First

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity framework code first, get access to foreign key value

From Dev

How to specify a foreign key with code-first Entity Framework

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Entity Framework 7 Foreign Key Column Name

From Dev

Entity Framework: Specify foreign key constraint name

From Dev

Entity Framework Code First, FullTextIndex & Inheritance

From Dev

Code First entity framework and foreign keys

From Dev

Entity Framework Code First initializing foreign keys

From Dev

Code first foreign key association MVC Entity

From Dev

Include foreign key in composite primary key in Code-First Entity Framework

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

From Dev

Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

Related Related

  1. 1

    Entity Framework Code First and Firebird - Foreign Key name issue

  2. 2

    Entity Framework Code First Foreign Key issue

  3. 3

    Entity Framework Code First Foreign Key issue

  4. 4

    Foreign Key in Code First Entity Framework

  5. 5

    Entity Framework: Foreign Key in code first

  6. 6

    Entity Framework Inheritance Code First

  7. 7

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  8. 8

    Entity Framework Code First Foreign Key adding Index as well

  9. 9

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  10. 10

    Entity Framework Code first adds unwanted foreign key column

  11. 11

    Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

  12. 12

    Multiple Foreign Key for Same table in Entity Framework Code First

  13. 13

    Get foreign key value using Entity Framework code first

  14. 14

    Entity Framework one to optional foreign key code first fluent mapping

  15. 15

    Entity framework code first cant create primary and foreign key relationship

  16. 16

    Entity framework code first, get access to foreign key value

  17. 17

    How to specify a foreign key with code-first Entity Framework

  18. 18

    Exchanged Foreign Key on Entity Framework Code First From data base

  19. 19

    Entity Framework 7 Foreign Key Column Name

  20. 20

    Entity Framework: Specify foreign key constraint name

  21. 21

    Entity Framework Code First, FullTextIndex & Inheritance

  22. 22

    Code First entity framework and foreign keys

  23. 23

    Entity Framework Code First initializing foreign keys

  24. 24

    Code first foreign key association MVC Entity

  25. 25

    Include foreign key in composite primary key in Code-First Entity Framework

  26. 26

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  27. 27

    Entity Framework 6 multiple table to one foreign key relationship code first

  28. 28

    Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

  29. 29

    Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

HotTag

Archive