3

I have the following tables: Country, Language and Country2Language

Country2Language is the linking table that creates the M:M relationship which is defined using Fluent Mapping:

mb.Entity<Country>()                             
  .HasMany(e => e.Languages)
  .WithMany(set => set.Countries)                
  .Map(mc =>
         {
           mc.ToTable("Country2Language");
           mc.MapLeftKey("CountryShortName");
           mc.MapRightKey("LanguagesID");                            
         }
      );  

My question: How can I add an additional DateTime "DateCreated" property?

Mike
  • 1,992
  • 4
  • 31
  • 42
  • 1
    Another example: http://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table/7053393#7053393 Basically it means that you can't map this as a many-to-many relationship anymore. Instead you need two one-to-many relationships with an additional entity in between. – Slauma Nov 02 '11 at 20:31

2 Answers2

1

You have to create an entity for your mapping that maps to the table.

public class Country2Language {
    [Key] 
    [Column(Order = 0)] 
    [ForeignKey("CountryShortName")] 
    public int CountryShortname{ get; set; } 

    [Key] 
    [Column(Order = 1)] 
    [ForeignKey("LanguagesID")] 
    public int LanguagesID { get; set; } 

    public DateTime DateCreated {get; set;}
}
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • You mean I have to add the class (entity) Country2Language to my project with only one property "DateCreated"? – Mike Nov 02 '11 at 18:57
  • @Mike - No, you also have to include the foreign keys, but you can still map them fluently if you want. – Erik Funkenbusch Nov 02 '11 at 19:03
  • 2
    @Mike - By the way, a great tool is the Entity Framework Power Tools CTP1. You can create a sample database with the structure you want, then use it to reverse engineer it to code-first. That will allow you to see how it should be defined in your real code first model. – Erik Funkenbusch Nov 02 '11 at 19:05
  • 1
    @Mysetere Man: Thanks for your answer. Unfortunately your solution doesn't work: "The ForeignKeyAttribute on property 'CountryShortname' on type 'EFCodeFirstTest.Country2Language' is not valid. The navigation property 'CountryShortName' was not found on the dependent type 'EFCodeFirstTest.Country2Language'. The Name value should be a valid navigation property name." – Mike Nov 02 '11 at 19:21
  • @Mike - I was just giving you an example, since i don't know how your real database is setup. You need to adapt it. – Erik Funkenbusch Nov 02 '11 at 19:23
  • I made several tests an unfortunately this example does not work :-/ – Mike Nov 02 '11 at 19:51
  • 1
    @Mike: You need navigation properties like in this example: http://stackoverflow.com/questions/7448229/entity-framework-code-1st-mapping-many-to-many-with-extra-info/7448834#7448834 The `ForeignKey` attribute on the FK property must refer to the navigation property and not to itself. – Slauma Nov 02 '11 at 20:27
1

Would it work to create the table with the created column and then set the default value in the database to the GetTime function (or w/e it is)? Then just use the left/right mapping and let the db automatically handle the default value for created?

I've had issues in the past where EF wants to insert null into columns if you don't specify the column is a db generated value, but the fact this is for a many-to-many relationship it probably wouldn't even know it was there and should always let it become the default.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • Glad this was a simple work around as opposed to having to take on the onus to maintain a many-to-many relationship manually. – Chris Marisic Mar 08 '12 at 22:06