I try to solve following:
In the entities HRCard and BPCard I have properties
public int DefaultAddressId { get; set; }
[ForeignKey("DefaultAddressId")]
public AddressDetail Address { get; set; } // table AddressDetail
so far no problem, now my Problem: In BPCard i have additionally a property:
public virtual ICollection<AddressDetail> Addresses { get; set; } //table AddressDetail
following the complete Code:
public abstract class EntityBase : IEntityModel {
[Key]
public int EntityId { get; set; }
[Required]
[StringLength(50)]
public string EntityKey { get; set; }
//...
}
// table HRCards
public class HRCard : EntityBase {
//Id from base class
// working fine
//...
public int DefaultAddressId { get; set; }
[ForeignKey("DefaultAddressId")]
public AddressDetail Address { get; set; } // table AddressDetail
}
// table BPCards
public class BPCard : EntityBase {
//Id from base class
// working fine
//...
public int DefaultAddressId { get; set; }
public int DefaultContactId { get; set; }
//working fine
[ForeignKey("DefaultAddressId")]
public AddressDetail DefaultAddress { get; set; } //table AddressDetail
//how can i solve this??
// table AddressDetail
public virtual ICollection<AddressDetail> Addresses { get; set; }
}
public class AddressDetail : EntityBase {
//Id from base class
// working fine
//...
public int ParentId { get; set; }
}
I have long time searched, but no result solve my problem really. My 1st solution was split the table into HRAddress and BPAddress this is working fine.
Edit: If I start enable migrations I get an error message:
"The property 'ParentId' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type."
many thanks
PS: can I change the tags later for better mapping?
It depends on the expected relationship you want with the Address POCO
You can solve the relationship using the annotation with something like
public class AddressDetail : EntityBase {
//Id from base class
public virtual ICollection<BPCard> Addresses { get; set; }
//public virtual BPCard Addresses { get; set; }
public virtual ICollection<HRCard> Addresses { get; set; }
//public virtual BPCard Addresses { get; set; }
}
or directly on your model with something like
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//one-to-many
modelBuilder.Entity<HRCard>()
.HasMany<AddressDetails>(s => s.Id)
.WithRequired(s => s.HRCard)
.HasForeignKey(s => s.AddressId);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.