public class ParentEntity
{
[Key]
public string ParentEntityID { get; set; }
// Navigation Properties
public virtual ICollection<ChildEntity> ChildEntities { get; set; }
public virtual ICollection<ChildEntitySubclass> ChildEntitySubclasses{ get; set; }
}
public class ChildEntity
{
[Key, Column(Order = 0)]
public string ParentEntityID { get; set; }
[Key, Column(Order = 1)]
public string CompositeKeyExtraPart { get; set; }
// Navigation Properties
[InverseProperty("ChildEntities")]
[ForeignKey("ParentEntityID ")]
public virtual ParentEntity ParentEntity { get; set; }
}
[Table("ChildEntitySubclass")]
public class ChildEntitySubclass : ChildEntity
{
public int DummyExtraProp { get; set; }
}
Updating because this problem is different than I though. As it seems, it is impossible to get the second navigation property (ChildEntitySubclasses) on ParentEntity to work. Can anyone confirm this?
Updating because this problem is different than I though. As it seems, it is impossible to get the second navigation property (ChildEntitySubclasses) on ParentEntity to work. Can anyone confirm this?
Hello:D
No,I can get the second navigation property。Please check this sample(you are not right and I cannot confirm)……
Thanks for the reply and the attention to this, however I think the reason this works, is only because you initialize the parent item with the list of subclasses inside it. IE, if you go over to the database, you will find that the subclass table has an
additional foreign key directly pointing to ParentEntity, which is redundant, but the way you insert the item this field is filled automatically.
So for the test to fail, you have to insert the ParentEntity first, then on dbg.ChildEntitySubclasses insert the respective items and on dbg.ChildEntity. You will see that they wont be related because the ParentEntity_ParentEntityID property (database side)
on them will be null.
So for the test to fail, you have to insert the ParentEntity first, then on dbg.ChildEntitySubclasses insert the respective items and on dbg.ChildEntity. You will see that they wont be related because the ParentEntity_ParentEntityID property (database side)
on them will be null.
Hello:)
As far as I see,One-To-Many relationship you should do to create a "One" instance part,and then do to create the instance of "Many" part and assign to the "Many" entitySet for the "One"。
bagosm
you will find that the subclass table has an additional foreign key directly pointing to ParentEntity, which is redundant,
Yes I suggest you removing it,it's really redundant……
Thanks for the follow-up. I dont think you understood what I said before, let me say it more clearly.
In the example you made, on the SQL Server side, you will get this:
table: ChildEntitySubclass
DummyExtraProp
K FK1 ParentEntityID
K FK1 CompositeKeyExtraPart
FK2 ParentEntity_ParentEntityID // This shouldn't exist! Duplicate FK to the parent Entity
bagosm
Member
5 Points
22 Posts
Code first, navigation property on subclassed entity
Jun 29, 2012 08:20 PM|LINK
Entities:
public class ParentEntity { [Key] public string ParentEntityID { get; set; } // Navigation Properties public virtual ICollection<ChildEntity> ChildEntities { get; set; } public virtual ICollection<ChildEntitySubclass> ChildEntitySubclasses{ get; set; } } public class ChildEntity { [Key, Column(Order = 0)] public string ParentEntityID { get; set; } [Key, Column(Order = 1)] public string CompositeKeyExtraPart { get; set; } // Navigation Properties [InverseProperty("ChildEntities")] [ForeignKey("ParentEntityID ")] public virtual ParentEntity ParentEntity { get; set; } } [Table("ChildEntitySubclass")] public class ChildEntitySubclass : ChildEntity { public int DummyExtraProp { get; set; } }Updating because this problem is different than I though. As it seems, it is impossible to get the second navigation property (ChildEntitySubclasses) on ParentEntity to work. Can anyone confirm this?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Code first, navigation property on subclassed entity
Jul 01, 2012 01:10 AM|LINK
Hello:D
No,I can get the second navigation property。Please check this sample(you are not right and I cannot confirm)……
bagosm
Member
5 Points
22 Posts
Re: Code first, navigation property on subclassed entity
Jul 01, 2012 10:31 AM|LINK
Hello decker!
Thanks for the reply and the attention to this, however I think the reason this works, is only because you initialize the parent item with the list of subclasses inside it. IE, if you go over to the database, you will find that the subclass table has an additional foreign key directly pointing to ParentEntity, which is redundant, but the way you insert the item this field is filled automatically.
So for the test to fail, you have to insert the ParentEntity first, then on dbg.ChildEntitySubclasses insert the respective items and on dbg.ChildEntity. You will see that they wont be related because the ParentEntity_ParentEntityID property (database side) on them will be null.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Code first, navigation property on subclassed entity
Jul 02, 2012 01:25 AM|LINK
Hello:)
As far as I see,One-To-Many relationship you should do to create a "One" instance part,and then do to create the instance of "Many" part and assign to the "Many" entitySet for the "One"。
Yes I suggest you removing it,it's really redundant……
bagosm
Member
5 Points
22 Posts
Re: Code first, navigation property on subclassed entity
Jul 02, 2012 09:49 AM|LINK
Hello!
Thanks for the follow-up. I dont think you understood what I said before, let me say it more clearly.
In the example you made, on the SQL Server side, you will get this:
table: ChildEntitySubclass DummyExtraProp K FK1 ParentEntityID K FK1 CompositeKeyExtraPart FK2 ParentEntity_ParentEntityID // This shouldn't exist! Duplicate FK to the parent EntitySo if you do on your test:
dbg.ParentEntities.Add(new ParentEntity { ParentEntityID = "1"}); dbg.ChildEntities.Add(new ChildEntity { ParentEntityID = "1", CompositeKeyExtraPart = "Test" } }); dbg.ChildEntitySubclasses.Add(new ChildEntitySubclass { ParentEntityID = "1", CompositeKeyExtraPart = "Test2", DummyExtraProp = 1 }); dbg.SaveChanges();Test will fail because FK2 is empty => (pe.ChildEntitySubclasses == null) BUT (pe.ChildEntities.Count == 2).
So what I have learnt is I have to do is:
public class ParentEntity { [Key] public string ParentEntityID { get; set; } // Navigation Properties public virtual ICollection<ChildEntity> ChildEntities { get; set; } public IEnumerable<ChildEntitySubclass> ChildEntitySubclasses { get { return ChildEntities.OfType<ChildEntitySubclass>(); } } }This works! But ChildEntities = ChildEntities + ChildEntitySubclasses. Is there a way to get only ChildEntity base classes? What I have done is:
public IEnumerable<ChildEntity> ChildEntityOnlyBaseClasses { get { return AllComponentPages.Where(r => !(r is ChildEntitySubclass)); } } }But using "! is ChildEntitySubclass" is not right, I need something like "Where R is ONLY-THE-BASE of ChildEntity".
Summing up: Problem now is mainly this, how can I get a List<ChildEntity> with NO ChildEntitySubclass?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Code first, navigation property on subclassed entity
Jul 03, 2012 01:52 AM|LINK
I'm afraid you cannot, because your ChildEntitySubclasses inherit from ChildEntitiy class.
bagosm
Member
5 Points
22 Posts
Re: Code first, navigation property on subclassed entity
Jul 03, 2012 06:45 PM|LINK
I was afraid this would be the case. Thank you for the help!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Code first, navigation property on subclassed entity
Jul 04, 2012 01:02 AM|LINK
Welcome to our forum next time……XD