i tried the whole day to get this working. I learned a lot about EF's Fluent API (e.g. this is an excellent article), however i had no success.
I have three Entities:
public class Address
{
[Key]
public virtual int AddressId { get; set; }
public virtual string AddressString { get; set; }
}
public class User
{
[Key]
public virtual int UserId { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
public class House
{
[Key]
public virtual int HouseId { get; set; }
public virtual Address Address { get; set; }
}
and tried all combinations of HasMany, HasOptional, WithOptional, WithOptionalDependent
and WithOptionalPrincipial
i could think of for both User
and House
in the
protected override void OnModelCreating(DbModelBuilder modelBuilder)
I just cannot get it to work. I think it should be clear, what i want. A User may have more than one address (in the first place i want to force at least one, but now i would be happy if a user may have the addresses optional...) while a House has exactly one Address - and this is required. It would be nice if the address of a house would be cascading deleted.