12

Edited with the new situation per suggestion in the comments:

Currently I have this mapping

public ShowMap() {
        ToTable("Shows");
        HasKey(x => x.ShowID);

        Property(x => x.ShowID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .IsRequired()
            .HasColumnName("ShowID");
}

public EpisodeMap() {
        ToTable("Episodes");
        HasKey(x => x.EpisodeID);

        Property(x => x.EpisodeID)
        .IsRequired()
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
        .HasColumnName("EpisodeID");

        Property(x => x.ShowID)
            .IsRequired()
            .HasColumnName("ShowID");

        Property(x => x.EpisodeNumber)
            .IsRequired()
            .HasColumnName("EpisodeNumber");
}

This results in the following database:

Episodes

Shows

However, when the seed method is run I receive this error. Since I can't debug the value of variables from the command line command Update-Database (at least, not as far as I know) I can't see what the property holds.

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

When I add the relationship to the mapping, I receive the following error:

One or more validation errors were detected during model generation:

System.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Episode_Show_Source' in relationship 'Episode_Show'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

Relationship:

        HasRequired(x => x.Show)
            .WithMany(x => x.Episodes)
            .HasForeignKey(x => x.EpisodeID);

This is the model:

public class Episode {
  public int EpisodeID {get; set;}
  public int ShowID {get; set;}
  public int EpisodeNumber {get; set;}
  public virtual Show Show { get; set; }
}

public class Show {
  public int ShowID {get; set;}
  public virtual ICollection<Episode> Episodes { get; set; }
}

What have I overlooked that causes validation errors?

Edit: just to be certain I haven't forgotten anything, this is the project on github.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • Why the need to have an compmosite key? new { x.ParentID, x.ChildID }) Is uniqueness only established by this table having both keys present? – ckross01 Jun 12 '13 at 19:20
  • The uniqueness of a record in the Child table is determined by the ParentID and a ChildID. The real world usage applicable here is a Show and its episodes: A show has multiple episodes, but each episode only belongs to one show. Would you suggest a different approach? – Jeroen Vannevel Jun 12 '13 at 19:30
  • 1
    Essentially it looks like you are telling EF to map ChildId twice, as a composite key and as a FK. Remove the ChildId and Parent id from the POCO. EF can figure that out. And if possible declare a unique single PK on Child (entity seed) to not have to use a composite. – ckross01 Jun 12 '13 at 19:41
  • I have refactored my program, but the error persists. I have updated the post to reflect the changes. – Jeroen Vannevel Jun 12 '13 at 22:30
  • Is this really *exactly* your model and mapping? That's a standard one-to-many relationship that everyone has used a hundred times without problems. The exception makes no sense at all. – Slauma Jun 13 '13 at 10:51
  • I looked over everything I posted here and couldn't find a difference. Just to be sure I have edited my post with the link to the github project since it might bring something to light that I forgot to include. – Jeroen Vannevel Jun 13 '13 at 10:58
  • In your public class Episode what is virtual Show Show for? Is it a pointer back to the public class Show? If it is, you shouldn't need it because you have the relationship established with ShowID. – nocturns2 Jun 13 '13 at 10:59
  • Yes. I'd like to be able to access the properties of a show from an episode. – Jeroen Vannevel Jun 13 '13 at 10:59
  • What do you mean? Should I just leave it out? How would I access the show's properties then? – Jeroen Vannevel Jun 13 '13 at 11:08

1 Answers1

20

This is the problem (from your git repository, class EpisodeMap):

HasRequired(x => x.Show)
    .WithMany(x => x.Episodes)
    .HasForeignKey(x => x.EpisodeID);

EpisodeID is the PK in Episode and EF expects a one-to-one mapping in this case without an Episodes collection but an Episode reference instead ("upper bound of multiplicity = 1").

For a one-to-many relationship it has to be:

HasRequired(x => x.Show)
    .WithMany(x => x.Episodes)
    .HasForeignKey(x => x.ShowID);
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • That did indeed solve the relationship related error, thanks! I've changed it and now the tables are created again. I still receive the entity validation error on the seed method though (Github: https://github.com/Vannevelj/NoName/blob/master/NoName/NoName/Migrations/Configuration.cs) – Jeroen Vannevel Jun 13 '13 at 11:42
  • Solved! The error was as it said: a validationerror. I've gone over my test data and the mapping and noticed the maxlength of one of the properties is 128. The seed data contained way more than that, setting it to 1028 solved the problem and I received my testing data. – Jeroen Vannevel Jun 13 '13 at 12:07
  • 1
    @JeroenVannevel: Just as a tip for the next time when you can't debug the validation errors: Call `SaveChanges` at the end of the Seed method manually in a try-catch block and write the errors into a file, like shown here: http://stackoverflow.com/a/16619043/270591 – Slauma Jun 13 '13 at 14:34
  • 1
    Very good idea, I'll add this to my project. In retrospect, the error message actually told me what was wrong, I should've found it way sooner. – Jeroen Vannevel Jun 13 '13 at 15:28
  • For my E-R model I didn't have the field at all corresponding to your `ShowID` field in my POCO classes. I was using code first development approach. It works. I was under an impression that fluent configuration APIs will be able to achieve it on its own while creating the database for the first time. – RBT Jun 05 '16 at 02:14