2

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

  • Your mistaking inheritance and foreign key concept, they are not the same, plus, this is not code first : these are partial classes, so it's DB first... – Antoine Pelletier Dec 07 '16 at 16:16
  • well... it looks like db first, and partial classes does not represent the table, it's just an ADD up, it's your model you need to change – Antoine Pelletier Dec 07 '16 at 16:22
  • There is no mistaking : I created the DB first and built a first version of my app upon it, using DB first. In this version, the model implemented inheritance of Entity -> HubEntity based on the two tables. For some reasons, I decided to rebuild the app from scratch using code first, which I'm less familiar with. I want to achieve inheritance Table Per Type, my only concern being this foreign key. I don't mix the foreign key concept on the db side and the inheritance on the code side, but I know that with TPT, the inheritance in the code is materialized by a foreign key in the table. – Cédric Bourgeois Dec 07 '16 at 19:49
  • The partial classes come from the code generated by the reverse engineering made by visual studio when implementing code-first on existing db. – Cédric Bourgeois Dec 07 '16 at 19:54
  • you can't build a db first aproach only with partial classes, what is your model looking like ? Like this guy : http://stackoverflow.com/questions/40937111/data-displaying-null-types/40938410?noredirect=1#comment69099399_40938410 look at his db first model... – Antoine Pelletier Dec 07 '16 at 20:34
  • I am building a code first approach with reverse engineering. I edited my question to add models and removed _partial_ from the classes. – Cédric Bourgeois Dec 08 '16 at 06:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130112/discussion-between-cedric-bourgeois-and-antoine-pelletier). – Cédric Bourgeois Dec 08 '16 at 12:38

1 Answers1

0

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. :)