5

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Martin Booka Weser
  • 3,192
  • 5
  • 28
  • 41

2 Answers2

4

I believe the following should work for you

public class Address
{
    public int AddressId { get; set; }
    public string AddressString { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
}

public class House
{
    public int HouseId { get; set; }
    public virtual Address Address { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<House> Houses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasMany(u => u.Addresses).WithMany();
        modelBuilder.Entity<House>().HasRequired(h => h.Address).WithOptional().Map(m => m.MapKey("AddressId"));
    }
}

Note that it's often better to specify the foreign key fields yourself, which can make life a lot easier for you later on. If you do this, then you can choose to rewrite House as the following:

public class House
{
    public int HouseId { get; set; }
    public int AddressId { get; set; }
    public virtual Address Address { get; set; }
}

Convention will link up AddressId and Address. If you have a one-to-one mapping between House and Address, you could also link them on their primary keys:

public class House
{
    [ForeignKey("Address")]              
    public int HouseId { get; set; }
    public virtual Address Address { get; set; }
}

You mentioned that you would like to enforce at least one address - this isn't possible with a one-to-many relationship. You could only do this if a user had exactly one address, at which point you could add a required AddressId property on the User class.

One other comment - you made everything virtual in your code. You only need to make navigation properties virtual.

Richard
  • 29,854
  • 11
  • 77
  • 120
  • well, today with more sleep, i figured that my problem was in the initializer, not in the context... however, your answer is valuable for me and hopefully for others, too. – Martin Booka Weser Nov 07 '12 at 09:42
1

What about something like this:

Enforce an Address for a House:

modelBuilder.Entity<House>().HasRequired(d => d.Address);

To create a one-to-many relationship between User and Address:

modelBuilder.Entity<User>().HasMany(u => u.Addresses).WithMany().Map(x =>
{
    x.MapLeftKey("UserId");
    x.MapRightKey("AddressId");
    x.ToTable("UserAddress");
});

What this should do is create a table called UserAddress that maps a User to an Address.

Alternatively, you could create a POCO for UserAddress yourself and modify the tables in question:

public class UserAddress
{
    [Key]
    public int UserAddressId { get; set; }
    public User User { get; set; }
    public Address Address { get; set; }
}

public User
{
    ...
    public virtual ICollection<UserAddress> UserAddresses { get; set; }
    ...
}

public Address
{
    ...
    public virtual ICollection<UserAddress> UserAddresses { get; set; }
    ...
}

You will then need the following to ensure that both User and Address are required:

modelBuilder.Entity<UserAddress>().HasRequired(u => u.Address);
modelBuilder.Entity<UserAddress>().HasRequired(u => u.User);
nick_w
  • 14,758
  • 3
  • 51
  • 71