0

I noticed that yet no one implemented start DB structure using Code-First.

Here is an example:

We have model Log (Id, Message) and we have model A1 (Id, Name, ICollection<Log> Logs)

When you have only A1 then EF Migration script will create A1_Id in Log table

Problem happens when we have a lot of Models with ICollection<Log> Logs because each model will add own "_Id" column to Log Table.

Is there a way to implement this structure/tables

A1 (Id, Name, ... ,  ICollection<Log> Logs)
A2 (Id, Name, ... ,  ICollection<Log> Logs)
....
A102 (Id, Name, ... ,  ICollection<Log> Logs)

Log (Id, Message, OwnerId)

and so every time we need Logs for record in A77

EF will do this (select * from Log where OwnerId = 'id of record from A77')

I also don't want to create 102 foreign key constrains.

Does anyone know how to do this? I guess it has to be somehow done in protected override void OnModelCreating(DbModelBuilder modelBuilder) method.

Cherven
  • 1,101
  • 3
  • 17
  • 26
  • 1
    Do you really need to see the log very often? I would remove any referential constraint related to the log table, make it a stand alone table. Please refer to [this post](http://stackoverflow.com/questions/5794417/audit-tables-maintaining-referential-integrity-good-or-bad) as reference. – Yuliam Chandra Sep 12 '14 at 20:26
  • Yeah, and do you really want a separate TABLE for each log collection? There has to be a saner way than that... – danludwig Sep 12 '14 at 20:27

0 Answers0