简体   繁体   English

指定的MVC C#模式无效

[英]MVC C# Schema specified is not valid

I'm working on my final project with Visual Studio 2015 with C#. 我正在使用带有C#的Visual Studio 2015进行最终项目。 I'm doing an app that works as simulator of an Android Game, and I have some characters (models.Personaje). 我正在做一个可用作Android游戏模拟器的应用,并且我有一些角色(models.Personaje)。 And those characters have abilities (BB) and when I want to do the migrations it threws: 这些角色具有能力(BB),当我想进行迁移时,它会抛出:

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. 错误:由于类型'Projecte_Final.Models.BB'不可用,因此未加载关系'Projecte_Final.Models.Personaje_BB'。

I don't know what am I doing wrong, here are the models BB and Personaje. 我不知道我在做什么错,这是BB和Personaje模型。

BB Model BB模型

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和Personaje模型的db上下文的代码:

//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. 因为项目仅在我的本地上,所以我去了数据库,删除了MigrationHistory表,还删除了所有迁移类,然后再次执行迁移-所以基本上我已经创建了新的Initial迁移。

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. 当您删除迁移历史记录时,EF将无法将您的当前模型与以前的版本进行比较,并且您的当前状态基本上是“初始”状态。

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: 我找到了解决方案,因为我和BB和Personaje之间有三重关系:

//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. 因为如果模型对同一个实体具有多个外键,则每个外键必须具有各自的反向导航属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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