Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a database that has a many to many between 2 tables. This works fine. I now need to add a second many to many between the same tables. So, following the same approach as with the first relationship, compiling is not problem but adding a migration to support the schema change gives me an error.

First relationship definition:

C#
modelBuilder.Entity<Hazard>()
    .HasMany(r => r.RiskEliminationRecommendations)
    .WithMany(h => h.Hazards)
    .Map(
    h =>
    {
        h.MapLeftKey("Hazard_id");
        h.MapRightKey("Option_id");
        h.ToTable("HazardEliminationOptions");
    });


uses the Hazard and RiskEliminationOption models (I removed the rest of the class for brevity's sake!):

(Hazard class)

public virtual ICollection<RiskEliminationOption> RiskEliminationRecommendations { get; set; }




(RiskEliminationOption class)

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RAdb.Models
{
    public class RiskEliminationOption
    {
        public int Id { get; set; }
        public string ResourceId { get; set; }
        public string Description { get; set; }
        public bool Active { get; set; }
        public virtual ICollection<Hazard> Hazards { get; set; }
    }
}


If I now add a further many-to-many definition in the OnModelCreating function of my DB Context class as follows:

C#
modelBuilder.Entity<Hazard>()
    .HasMany(o => o.NewEliminateRiskRecommendations)
    .WithMany(h => h.Hazards)
    .Map(
    h =>
    {
        h.MapLeftKey("Hazard_id");
        h.MapRightKey("Option_id");
        h.ToTable("HazardNewRiskEliminationOptions");
    });


The application will build perfectly but when I come to scaffold the migration I get the following error:

PM> Add-Migration ReAssessment1
System.Data.Entity.Core.MetadataException: Schema specified is not valid. Errors: The relationship 'RAdb.Models.Hazard_RiskEliminationRecommendations' was not loaded because the type 'RAdb.Models.RiskEliminationOption' is not available.
   at System.Data.Entity.Core.Metadata.Edm.CodeFirstOSpaceTypeFactory.LogError(String errorMessage, EdmType relatedType)
   at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.CreateAndAddNavigationProperty(StructuralType cspaceType, StructuralType ospaceType, NavigationProperty cspaceProperty)
   at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.<>c__DisplayClass18.<TryFindNavigationProperties>b__14()
   at System.Data.Entity.Core.Metadata.Edm.CodeFirstOSpaceLoader.LoadTypes(EdmItemCollection edmItemCollection, ObjectItemCollection objectItemCollection)
   at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping)
   at System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace..ctor(DbDatabaseMapping databaseMapping)
   at System.Data.Entity.Infrastructure.DbCompiledModel..ctor(DbModel model)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Schema specified is not valid. Errors: The relationship 'RAdb.Models.Hazard_RiskEliminationRecommendations' was not loaded because the type 'RAdb.Models.RiskEliminationOption' is not available.
PM> 


Can anyone explain to me what I am doing wrong here?

Many thanks in advance,
Chris.
Posted
Comments
Maciej Los 8-Mar-16 2:17am    
Did you resolve your issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900