Having a one to many AND many to many relationship between the same entities? Is it possible without using fluentAPI?

Answered Having a one to many AND many to many relationship between the same entities? Is it possible without using fluentAPI?

  • Thursday, February 23, 2012 8:43 AM
     
      Has Code

    Hello,

    My Model:

        public class Bauteil {
            public Guid ID { getset; }
            public string Name { getset; }
     
            public Guid? BuildingID { getset; } //FK to the main Building (optional if in archive only)
            public virtual Building Building { getset; } 
     
            public virtual IList<Building> Buildings { getset; } //List of Buildings used in
        }
        public class Building {
     
            public Guid ID { getset; }
            public string Name { getset; }
     
            public virtual IList<Bauteil> Bauteile { getset; }
        }

    fluent API

            protected override void OnModelCreating(DbModelBuilder modelBuilder) {
                modelBuilder.Entity<Building>()
                    .HasMany<Bauteil>(building => building.Bauteile)
                    .WithMany(bauteil => bauteil.Buildings);
            }

    With this fluent API it is working.

    Are there some naming conventions or attributes to have the same result without the fluentAPI?

    I would really like having all the code in the Model itselfe... Because with the same Model classes I create many different Context Classes...

    kind regards,

    Markus

    One more thing:

    If I port the above working sample with the fluent API to my real project, where the Model is located in a different Assembly than the context I get the Error:

    Schema specified is not valid. Errors:
    (223,6) : error 0040: Type Anwesen_Bauteile is not defined in namespace MyTga.Web.DbContexts (Alias=Self).

    (Where Type Anwesen = Type Building from the sampe code above)

    (I use EF4.3)

    Edited: The Problem for the error above is not the different Assembly but the fact that the Navigation property is in a base class

    The following Model produces this error:

        public class Building {
     
            public Guid ID { getset; }
            public string Name { getset; }
     
            public virtual IList<Bauteil> Bauteile { getset; }
            public virtual IList<Dokument> Dokumente { getset; }
        }
    
        public class PartBase {
            public virtual IList<Building> Buildings { getset; } //List of Building used in
        }
        public class Bauteil:PartBase {
            public Guid ID { getset; }
            public string Name { getset; }
     
            public Guid? BuildingID { getset; } //FK to the main Building (optional if in archive only)
            public virtual Building Building { getset; } 
     
        }
        public class Dokument:PartBase {
            public Guid ID { getset; }
            public string Name { getset; }
     
            public Guid? BuildingID { getset; } //FK to the main Building (optional if in archive only)
            public virtual Building Building { getset; }
     
        }
            protected override void OnModelCreating(DbModelBuilder modelBuilder) {
                modelBuilder.Entity<Building>()
                    .HasMany<Bauteil>(building => building.Bauteile)
                    .WithMany(bauteil => bauteil.Buildings);
                modelBuilder.Entity<Building>()
                    .HasMany<Dokument>(building => building.Dokumente)
                    .WithMany(dokument => dokument.Buildings);
            }
    

    If I don't use PartBase as a base class it is working....



All Replies