88475

Can I create a separate table for a single Collection property using Entity Framework code first?

I have a class Foo with an ICollection<string>. To be able to store this through Entity Framework, I have created a class Bar with an Id and Value property. Entity Framework creates a table for Bar with a foreign key column to Foo. This works well.

The problem is that I have a whole bunch of classes with such a ICollection<string>. I converted them all to ICollection<Bar>. The result however is that the Foo table now contains a foreign key column for every class that has an ICollection<Bar> (10+).

While I understand that it's necessary to do this to have those relationships work in the database, the database diagram doesn't get more intuitive this way.

I was thinking that, like in a many to many relationship, I would like to have a table in between each of those relationships. For example,

    <li>a table Foo_Bar with two columns: Foo_Id and Bar_Id for the relationship between Foo and Bar and another table Baz_Bar with Baz_Id and Bar_Id.</li> </ul>

    To achieve this, I can create a sub class of Bar (e.g. FooBar) for every relationship; change the collection to use those (e.g. ICollection<FooBar>); and let EF create the database using the table per type (e.g. by decorating FooBar with [Table("Foo_Bar")]) approach for these types. But I would like to prevent having to create all these empty classes.

    I wonder if it is possible to somehow specify this behaviour just based on the property. Something like this:

    public class Foo { [Table("Foo_Bar")] public ICollection<Bar> Bars { get; set; } }

    On a side note: If this approach is a bad idea in the first place, don't hesitate to let me know :).

    Answer1:

    to summarize comments:

    what you want (i.e.: 5 tables) is a many to many relation for Entity Framework.

    By conventions you get it by setting a collection on both entities

    Foo { ICollection<Bar> Bars } Foo { ICollection<Foo> Foos }

    if you don't want the reverse collections (Foos) then you have to setup the relation by the fluent API.

    modelBuilder.Entity<Foo>.HasMany(x => x.Bars).WithMany()

    AFAIK there is no annotation allowing to reach this last result by using the Annotations.

    You then have to handle by hand the unicity of Foo for a Bar. A solution may be a unique constraint on the Bar Key of the join table created by EF.

Recommend

  • Can I change DB table name from the default automatically on Django?
  • mysql aggregate UDF (user defined function) in C
  • Apply function to each cell in DataFrame
  • Changing naming strategy of @Table and @Column with EclipseLink to lower-case
  • Go Unmarshaling YAML into struct
  • Red-Black Tree Delete Fixup in CLRS Second Edition, in Clojure
  • How to get Email address from outlook contacts for the names listed in a column?
  • @IfProfileValue import set from env variable, test not executing
  • UML Sequence Diagram Message Branching
  • UML specification : Do I need inheritance?
  • iOS autolayout - gap between image and top of the screen, despite constraints set
  • strtotime returns incorrect timestamp for '-1 month'
  • Relational Database Mapping in MVC
  • Associate git repo with existing fork network
  • How to determine the association between a VB6 app and an exe instanced with CreateObject()
  • How to specify a multi-column UNIQUE constraint in code-first Entity Framework fluent API
  • Detaching entity along with referenced entities
  • R h2o.glm - issue with max_active_predictors
  • CoreData basics – to-many relationship array data
  • Layout design help Android
  • Client side validation mvc dropdown
  • CRASH: *** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 9]
  • WPF version of .ScaleControl?
  • Primefaces :radioButton inside a ui:repeat
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Breaking out column by groups in Pandas
  • uniform generation of points on 3D box
  • Unable to get column index with table.getColumn method using custom table Model
  • SharedPreferences or SQLite Database?
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Q promise. Difference between .when and .then
  • Using jQuery closest() method with class selector
  • Array.prototype.includes - not transformed with babel
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4