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
Hello,
My Model:
public class Bauteil { public Guid ID { get; set; } public string Name { get; set; } public Guid? BuildingID { get; set; } //FK to the main Building (optional if in archive only) public virtual Building Building { get; set; } public virtual IList<Building> Buildings { get; set; } //List of Buildings used in }
public class Building { public Guid ID { get; set; } public string Name { get; set; } public virtual IList<Bauteil> Bauteile { get; set; } }
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 { get; set; } public string Name { get; set; } public virtual IList<Bauteil> Bauteile { get; set; } public virtual IList<Dokument> Dokumente { get; set; } }
public class PartBase { public virtual IList<Building> Buildings { get; set; } //List of Building used in }
public class Bauteil:PartBase { public Guid ID { get; set; } public string Name { get; set; } public Guid? BuildingID { get; set; } //FK to the main Building (optional if in archive only) public virtual Building Building { get; set; } }
public class Dokument:PartBase { public Guid ID { get; set; } public string Name { get; set; } public Guid? BuildingID { get; set; } //FK to the main Building (optional if in archive only) public virtual Building Building { get; set; } }
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....
- Edited by M.Hopfenspirger Thursday, February 23, 2012 10:29 AM
All Replies
-
Friday, February 24, 2012 9:11 AMModerator
Hi M.Hopfenspirger,
Welcome!
Please refer my reply in this post: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/917b9207-17e8-4ee5-b33e-f621ef2a998a/
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Tuesday, March 06, 2012 9:02 AMModerator
Hi M.Hopfenspirger,
Sorry for missing your first question.
Using annotations for many to many as following:
public class Product { [Key] public int Id { get; set; } [InverseProperty("Products")] public virtual ICollection<Category> Categories { get; set; } } public class Category { [Key] public int Id { get; set; } [InverseProperty("Categories")] public virtual ICollection<Product> Products { get; set; } }BTW, I'd like to check the states of your question.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

- Marked As Answer by M.Hopfenspirger Tuesday, March 06, 2012 9:08 AM
- Edited by Alan_chenModerator Tuesday, March 06, 2012 9:12 AM spell
-
Tuesday, March 06, 2012 9:09 AM
Hello Alan,
thanks for the reply. This is perfect :-)
Nice greetings, Markus

