简体   繁体   中英

MVC C# Schema specified is not valid

I'm working on my final project with Visual Studio 2015 with C#. I'm doing an app that works as simulator of an Android Game, and I have some characters (models.Personaje). And those characters have abilities (BB) and when I want to do the migrations it threws:

Schema specified is not valid. Errors: The relationship 'Projecte_Final.Models.Personaje_BB' was not loaded because the type 'Projecte_Final.Models.BB' is not available.

I don't know what am I doing wrong, here are the models BB and Personaje.

BB Model

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

namespace Projecte_Final.Models
{
    public class BB
    {
    public int ID { get; set; }
    //Habilidades de los personajes
    public String Nombre { get; set; }
    public String BBDesc { get; set; }

    //Rama del BB (Heal, Support o Atk)
    public int RamaBBID { get; set; }
    public virtual RamaBB RamaBB { get; set; }

    //Tipo del BB (BB, SBB, UBB)
    public int TipoBBID { get; set; }
    public virtual TipoBB TipoBB { get; set; }

    //Grupalidad del BB
    public int GrupalBBID { get; set; }
    public virtual GrupalBB GrupalBB { get; set; }

    public int NhitsBB { get; set; }
    public int DCBB { get; set; }
    public int CosteBB { get; set; }
    public int MultiplicadorBB { get; set; }

    public int EfectoBBID { get; set; }
    public virtual Efectos EfectoBB { get; set; }
    public virtual ICollection<Personaje> Personajes { get; set; }
    }
}

Personaje Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Projecte_Final.Models
{
    public class Personaje
    {
    //Personaje del juego.

    //Datos generales
    public int Numero { get; set; }
    public String Nombre { get; set; }
    public int NivelMax { get; set; }
    public int Estrellas { get; set; }
    public int Coste { get; set; }

    public virtual ICollection<Stats> Stats { get; set; }

    //Elemento
    public int ElementoID { get; set; }
    public virtual Elemento Elemento { get; set; }

    //Genero
    public int GeneroID { get; set; }
    public virtual Genero Genero { get; set; }



    //Datos combate
    public int NHits { get; set; }
    public int DC { get; set; }


    //BB
    public int? BBID { get; set; }
    public virtual BB BB { get; set; }

    //SBB
    public int? SBBID { get; set; }
    public virtual BB SBB { get; set; }

    //UBB
    public int? UBBID { get; set; }
    public virtual BB UBB { get; set; }


    //Datos IMPS
    public int ImpHP { get; set; }
    public int ImpAtk { get; set; }
    public int ImpDef { get; set; }
    public int ImpRec { get; set; }


    //Descripciones extras
    public int? LSID { get; set; }
    public virtual LS LS { get; set; }

    public int? ESID { get; set; }
    public virtual ES ES { get; set; }


    //Pre i post evoluciones
    public int? PreEvoNum { get; set; }
    public virtual Personaje PreEvo { get; set; }
    public int? PostEvoNum { get; set; }
    public virtual Personaje PostEvo { get; set; }


    //Imágenes
    [NotMapped]
    public HttpPostedFileBase Imagen { get; set; }
    [NotMapped]
    public HttpPostedFileBase Icono { get; set; }
    [NotMapped]
    public HttpPostedFileBase Gif { get; set; }
    [NotMapped]
    public HttpPostedFileBase GifAtaque { get; set; }


    //Propiedad de navegacion propia
    public virtual ICollection<Personaje> Personajes { get; set; }

    //Propiedad de navegacion a Unidad
    public virtual ICollection<Unidad> Unidades { get; set; }
    }
}

And here it is the code for the db context for models BB and Personaje:

//BB
        modelBuilder.Entity<BB>().HasKey(x => x.ID);
        modelBuilder.Entity<BB>().HasRequired(x => x.RamaBB).WithMany(x => x.BBRama).HasForeignKey(x => x.RamaBBID);
        modelBuilder.Entity<BB>().HasRequired(x => x.TipoBB).WithMany(x => x.BBTipo).HasForeignKey(x =>x.TipoBBID);
        modelBuilder.Entity<BB>().HasRequired(x => x.GrupalBB).WithMany(x => x.BBGrupal).HasForeignKey(x => x.GrupalBBID); 
        modelBuilder.Entity<BB>().HasRequired(x => x.EfectoBB).WithMany(x => x.BBEfectos).HasForeignKey(x => x.EfectoBBID); 

//Personaje
        modelBuilder.Entity<Personaje>().HasKey(x => x.Numero);
        modelBuilder.Entity<Personaje>().HasRequired(x => x.Elemento).WithMany(x => x.Personajes).HasForeignKey(x => x.ElementoID);
        modelBuilder.Entity<Personaje>().HasRequired(x => x.Genero).WithMany(x => x.Personajes).HasForeignKey(x => x.GeneroID);
        modelBuilder.Entity<Personaje>().HasOptional(x => x.BB).WithMany(x => x.Personajes).HasForeignKey(x => x.BBID);
        modelBuilder.Entity<Personaje>().HasOptional(x => x.SBB).WithMany(x => x.Personajes).HasForeignKey(x => x.SBBID);
        modelBuilder.Entity<Personaje>().HasOptional(x => x.UBB).WithMany(x => x.Personajes).HasForeignKey(x => x.UBBID);
        modelBuilder.Entity<Personaje>().HasOptional(x => x.ES).WithMany(x => x.Personajes).HasForeignKey(x => x.ESID);
        modelBuilder.Entity<Personaje>().HasOptional(x => x.LS).WithMany(x => x.Personajes).HasForeignKey(x => x.LSID);
        modelBuilder.Entity<Personaje>().HasOptional(x => x.PreEvo).WithMany(x => x.Personajes).HasForeignKey(x => x.PreEvoNum);
        modelBuilder.Entity<Personaje>().HasOptional(x => x.PostEvo).WithMany(x => x.Personajes).HasForeignKey(x => x.PostEvoNum);

I hope that you can help me, thank you.

尝试更新模型,因为您可能首先创建了模型,然后对数据库进行了一些更改,而不是再次更新模型。

I had a similar issue before. Because the project was only on my local, I just went to the db, deleted MigrationHistory table, also deleted all migration classes, and executed migrations again - So basically I've created new Initial migration.

When you delete migrations history EF will not be able to compare your current model with previous version and your current state will be basically "initial" state.

Note

I didn't have any critical data inside my database and I was able to do that. So be careful. This is not recommended solution, but it does the job, especially if you are starting project or preparing starting point.

For more info I can recommend this post:

Reset Entity-Framework Migrations

I found the solution, I had a triple relationship within BB and Personaje cause I had:

//Personaje Model
        //BB
    public int? BBID { get; set; }
    public virtual BB BB { get; set; }

    ////SBB
    public int? SBBID { get; set; }
    public virtual BB SBB { get; set; }

    ////UBB
    public int? UBBID { get; set; }
    public virtual BB UBB { get; set; }

But I had:

//BB Model
public virtual ICollection<Personaje> Personajes { get; set; }

And it must be:

//BB Model
    public virtual ICollection<Personaje> PersonajesBB { get; set; }
    public virtual ICollection<Personaje> PersonajesSBB { get; set; }
    public virtual ICollection<Personaje> PersonajesUBB { get; set; }

Because if a model have more than 1 foreign key to the same entity, each foreign key have to have their respective reverse navigation property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM