19

This is similar to questions here and here, but those are old and have no good answers.

Let's say I have the following classes:

class HairCutStyle { 
   public int ID { get; set; }
   public string Name { get; set; }
}

class CustomerHairCutPreference {
   public int ID { get; set; }
   public Customer Customer { get; set; }
   public HairCutStyle HairCutStyle { get; set; }
}

Let's say my HairCutStyle data is stored in a table in another database (I get it from Paul Mitchell himself). I want to use the HairCutStyle class as a POCO class - something that I will use in code to represent hair cut styles, but I don't need to read/write that information in my database. (Assume I have a separate service layer that can populate the data for these classes from the other database.)

How can I tell EF NOT to create a HairCutStyle table in my current db context? But at the same time, I want to store a value in the CustomerHairCutPreference table that is a reference to the HairCutStyle data stored elsewhere. A "virtual" foreign key of sorts, that isn't constrained by an actual database FK constraint.

Community
  • 1
  • 1
Chris
  • 443
  • 1
  • 3
  • 14
  • 3
    Did you try not adding it to your DbContext class? That should mean that EF has no knowledge of the file. – krillgar Jun 04 '15 at 12:20
  • 2
    Possible duplicate. http://stackoverflow.com/questions/7642205/how-to-configure-ef-code-first-to-not-map-a-specific-type – Steve Danner Jun 04 '15 at 12:20

2 Answers2

30

Add a property in CustomerHairCutPreference for HairCutSytleID and then use the [NotMapped] attribute on the HairCutStyle property. Note, however, that you will then be responsible for ensuring that the HairCutStyle and HairCutStyleID stay in sync.

class CustomerHairCutPreference {
   public int ID { get; set; }
   public Customer Customer { get; set; }
   public int HairCutStyleID {get; set; }

   [NotMapped]
   public HairCutStyle HairCutStyle { get; set; }
}

Alternatively, you can use the FluentAPI to exclude HairCutStyle completely from ever being mapped by Entity Framework, which may be useful if you have multiple classes that link to it.

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    modelBuilder.Ignore<HairCutStyle>();
}
Claies
  • 22,124
  • 4
  • 53
  • 77
  • 1
    Your alternative answer was very helpful. In my case, I am working on a project that uses EF for existing tables, but we did not want to use EF anymore for the new tables that we were adding, that also used the same schema as the existing tables. I was getting an "EntityType has no key defined" error from EF for the new tables. Your answer helped me resolve it, thanks. – Dino Bansigan Jan 10 '19 at 18:04
8

There are three things to ensure:

  1. Make sure you do not expose a DbSet<HairCutStyle> in your DbContext-derived class
  2. Make sure you do not have any mention of HairCutStyle in your OnModelCreating override
  3. Mark your HairCutStyle property using the NotMapped attribute.
Jurgen Camilleri
  • 3,559
  • 20
  • 45