0

I'm not very experienced with EF and I'm trying to figure out what is the proper way or what are the options for creating entity, collection of which can be contained in other entities (different tables). Let's say I have three existing classes (ModuleA, ModuleB, ModuleD) that I want to contain its own collection of Data entities. I wanted a single table for Data entities but I don't like the idea of three nullable Guid columns for each FK. That also gives me error on applying migration ("...may cause cycles or multiple cascade paths") - which could by probably solved by removing cascade delete and deleting Data manually, but I don't like that idea.

What I would like most is the single (shared) Guid property on Data for FKs to all three modules, which is not possible at least not without same ID existing in all three main tables at the same time (since it creates three FKs in DB).

class Data
{
    public int Id {get; set;}
    public byte[] Values {get; set;}

}

class ModuleA
{
    public Guid Id {get; set;}
    public ICollection<Data> Data {get; set;}
    // some other stuff...  
}
class ModuleB
{
    public Guid Id {get; set;}
    public ICollection<Data> Data {get; set;}
    // ...
}
class ModuleC
{
    public Guid Id {get; set;}
    public ICollection<Data> Data {get; set;}
    // some different other stuff...
}
JankoHrasko
  • 93
  • 1
  • 8

1 Answers1

0

You can create 3 module-to-data many-to-many tables like this:

class Data
{
    public int Id {get; set;}
    public byte[] Values {get; set;}

}

class ModuleAData 
{
    public ModuleAId {get; set;}
    public DataId {get; set;}
}

class ModuleA
{
    public Guid Id {get; set;}
    public ICollection<ModuleAData> Data {get; set;}
    // some other stuff...  
}
shibormot
  • 1,638
  • 2
  • 12
  • 23
  • Thanks shibormot but that's 3 more tables just for the sake of having Data in one table. Then I'd rather go with 3 Data tables, to have 4 at the end instead of 7. Is there any other benefit in this approach? I assume data manipulation will not be very straightforward with this model. – JankoHrasko Mar 20 '16 at 00:02
  • One benefit is that you can use same data in different modules. Other: If Data have same meaning for all modules then on client you can malipulate it with single logic (some class that read and write to Values), with no need to overload this logic for different modules, because it will be 3 classes and you will need 3 overloads. If db and your .net logic will be always coupled it is not a problem. If logic is simple it is also not a problem. But if someone decide to rewrite client using 4 tables structure with complex Data logic on language without simple overloads (sql?). Dont know your app) – shibormot Mar 20 '16 at 00:32