3

I've got a database structure where I've got an Equipment table with the columns Equipment_Id, Field_1, and Field_2. I've got an Equipment_Locale table with the fields Equipment_Id and Desc. The Ids are the same in both tables, and there is a one-to-one relationship between these tables.

I've got the following entity:

public class Equipment
{
    public long Id { get; set; }
    public string Description { get; set; }
    public long Field1 { get; set; }
    public long Field2 { get; set; }
}

I've got the following EntityTypeConfiguration:

public class EquipmentMapping : EntityTypeConfiguration<Equipment>
{
    public EquipmentMapping()
    {
        ToTable("EQUIPMENT");
        HasKey(e => e.Id);
        Property(e => e.Id).HasColumnName("EQUIPMENT_ID");
        Property(e => e.Field1).HasColumnName("FIELD_1");
        Property(e => e.Field2).HasColumnName("FIELD_2");
        // TODO: Okay, now I need to get the description in here!
    }
}

I need to map the description in there, though, which comes from the EQUIPMENT_LOCALE table's DESC column.

This answer gives me a pretty clear idea on how I could use this if I was defining the mapping in ModelBuilder. However, we've been using files with EntityTypeConfigurations on this project and just having the model builder add those configurations, and I'm not sure how to set up a two table mapping in one of those. How can I accomplish this?

Community
  • 1
  • 1
Sterno
  • 1,638
  • 2
  • 17
  • 28

2 Answers2

5

It turns out that the answer I linked which did it in ModelBuilder was really, really close to what I needed to simply put in my EntityTypeConfiguration file. I'd just never used Map() in EntityTypeConfiguration before so I was a bit clueless.

The following seems to work for me:

public class EquipmentMapping : EntityTypeConfiguration<Equipment>
{
    public EquipmentMapping()
    {
        HasKey(e => e.Id);
        Property(e => e.Id).HasColumnName("EQUIPMENT_ID");
        Property(e => e.Field1).HasColumnName("FIELD_1");
        Property(e => e.Field2).HasColumnName("FIELD_2");
        Property(e => e.Description).HasColumnName("DESC");

        Map(m =>
        {
            m.Properties(e => new
            {
                e.Id,
                e.Field1,
                e.Field2
            });
            m.ToTable("EQUIPMENT");
        });

        Map(m =>
        {
            m.Properties(e => new
            {
                e.Id,
                e.Description
            });
            m.ToTable("EQUIPMENT_LOCALE");
        });
    }
}
Sterno
  • 1,638
  • 2
  • 17
  • 28
-1

You're going to need a navigation property in your parent:

public virtual Equipment_Locale Equipment_Locale { get; set; } 

Then you can add to equipment configuration mapping like:

        HasRequired(p => p.Equipment_Locale )
            .WithMany()
            .HasForeignKey(p => p.Equipment_LocaleId )
            .WillCascadeOnDelete(false);

See here for relationship mapping: https://msdn.microsoft.com/en-us/data/jj591620.aspx

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • I probably should have specified it's a one-to-one mapping. I'll update my question. – Sterno Feb 26 '15 at 21:01
  • The configuration syntax will depend on where you put the foreign key. See WithRequiredPrincipal or WithRequiredDependent in the link. – Steve Greene Feb 26 '15 at 21:09
  • This doesn't map two tables to one entity. It creates a navigation property to another entity. I didn't want to have to create another entity for what amounted to a string (Description). – Sterno Feb 26 '15 at 21:12