Having a one to many AND many to many relationship between the same entities? Is it possible without using fluentAPI?
-
2012년 2월 23일 목요일 오전 8:43
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....
- 편집됨 M.Hopfenspirger 2012년 2월 23일 목요일 오전 9:05
- 편집됨 M.Hopfenspirger 2012년 2월 23일 목요일 오전 10:29
모든 응답
-
2012년 2월 24일 금요일 오전 9:11중재자
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.

-
2012년 3월 6일 화요일 오전 9:02중재자
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.

- 답변으로 표시됨 M.Hopfenspirger 2012년 3월 6일 화요일 오전 9:08
- 편집됨 Alan_chenModerator 2012년 3월 6일 화요일 오전 9:12 spell
-
2012년 3월 6일 화요일 오전 9:09
Hello Alan,
thanks for the reply. This is perfect :-)
Nice greetings, Markus

