【问题标题】:Entity type<entity> does not match the model for the current context实体类型<entity> 与当前上下文的模型不匹配
【发布时间】:2017-04-26 07:03:15
【问题描述】:

我已经找了好几天了,还是没有找到答案,所以我问你们,希望你们能帮助我

我正在为我的实习构建一个软件,但我只是继续其他人的代码。问题是,2 天前,我使用 Visual Studio 2017 构建了该项目,它运行良好,但现在它不再工作了,当我尝试对某些关系做一些事情时,我得到了这个“实体类型......”错误(不是全部)

举个例子:

DAO.FichePreDiag f = BP.getFicheById(r.idFiche);

调用

try
        {
            using (var ctx = new HDPreDiagEntities())
            {
                return ctx.FichePreDiags.Where(f => f.id == idFiche).First();
            }
        }

这里是 HDPreDiag 实体类

public partial class HDPreDiagEntities : DbContext
{
    public HDPreDiagEntities()
        : base("name=HDPreDiagEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Equipement> Equipements { get; set; }
    public virtual DbSet<FichePreDiag> FichePreDiags { get; set; }
    public virtual DbSet<Text> Texts { get; set; }
    public virtual DbSet<TypePanne> TypePannes { get; set; }
    public virtual DbSet<User> Users { get; set; }
    public virtual DbSet<HistoriqueMail> HistoriqueMails { get; set; }
    public virtual DbSet<Pointage> Pointages { get; set; }
    public virtual DbSet<Rapport> Rapports { get; set; }
}

我不明白的是它起作用了,然后突然不起作用了,我不明白

感谢您的帮助

编辑:

FichePrediag 类:

public partial class FichePreDiag
{
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

    public int id { get; set; }
    public System.DateTime DateCreation { get; set; }
    public bool telMail { get; set; }
    public string interlocuteurHD { get; set; }
    public string nomClient { get; set; }
    public string classificationClient { get; set; }
    public bool commentaireClient { get; set; }
    public bool acreerClient { get; set; }
    public bool amodifierClient { get; set; }
    public string idClient { get; set; }
    public string nomInterlocuteur { get; set; }
    public string mailInterlocuteur { get; set; }
    public string telephoneInterlocuteur { get; set; }
    public bool acreerInterlocuteur { get; set; }
    public bool amodifierInterlocuteur { get; set; }
    public string identificationHD { get; set; }
    public string typeMachine { get; set; }
    public string cn { get; set; }
    public string identificationClient { get; set; }
    public bool acreerMachine { get; set; }
    public bool etiquetteMachine { get; set; }
    public bool amodifierMachine { get; set; }
    public bool maintenancePropose { get; set; }
    public Nullable<int> numIntervention { get; set; }
    public string technicienPrecedent { get; set; }
    public Nullable<int> degresUrgence { get; set; }
    public bool majorationUrgence { get; set; }
    public string panne { get; set; }
    public string descriptif { get; set; }
    public string typeContrat { get; set; }
    public Nullable<int> nbHeure { get; set; }
    public Nullable<System.DateTime> datePropose { get; set; }
    public Nullable<System.DateTime> dateConfirmee { get; set; }
    public string dialogueClient { get; set; }
    public string Equipements { get; set; }
    public string Fournitures { get; set; }
    public bool isArchived { get; set; }
    public Nullable<System.DateTime> LastMaintenance { get; set; }
    public string idInterlocuteur { get; set; }
    public string typepanne1 { get; set; }
    public string typepanne2 { get; set; }
    public string Techniciens { get; set; }
    public Nullable<System.DateTime> NextMaintenance { get; set; }
    public string motifAppel { get; set; }
    public string cds { get; set; }
    public string bi { get; set; }
    public string statusCDS { get; set; }
    public string fonctionInterlocuteur { get; set; }
    public List<string> NomDemandeAide { get; set; }
    public List<Nullable<System.DateTime>> DateTimeDemandeAide { get; set; }
    public string NomDemandeDevis { get; set; }
    public Nullable<System.DateTime> DateTimeDemandeDevis { get; set; }
    public string nomInterlocuteurSurPlace { get; set; }
    public string fonctionInterlocuteurSurPlace { get; set; }
    public string mailInterlocuteurSurPlace { get; set; }
    public string telephoneInterlocuteurSurPlace { get; set; }
    public Nullable<System.DateTime> DatePlanificationDeb { get; set; }
    public Nullable<System.DateTime> DatePlanificationFin { get; set; }
    public Nullable<decimal> tarifBiPrice { get; set; }
    public string tarifBiNom { get; set; }

    public string telMailString { get { return (this.telMail) ? "Téléphone" : "Mail/Fax"; } }
    public string typeModifClientString { get { return (this.acreerClient) ? "A CREER" : "A MODIFIER"; } }
    public string typeModifInterlocuteurString { get { return (this.acreerInterlocuteur) ? "A CREER" : "A MODIFIER"; } }
    public string commentaireString { get { return (this.commentaireClient) ? "Oui" : "Non"; } }
    public string refMachineHD { get { return this.identificationHD.Split(' ')[0]; } }

    public bool demandeAideFilled { get { return (this.NomDemandeAide != null); } }
    public bool demandeAideFilledAndItsMine { get { return (this.NomDemandeAide != null && isMine()); } }
    public bool demandeDevisFilled { get { return (this.NomDemandeDevis != null); } }
    public bool demandeDevisFilledAndItsMine { get { return (this.NomDemandeDevis != null && DAL.getUserByUsername(Environment.UserName).fullName.Equals(this.NomDemandeDevis)); } }

    /// <summary>
    /// Fonction qui permet de vérifier si un technicien a été sollicité pour de l'aide.
    /// </summary>
    public bool isMine() {/*
        foreach(string nomTechnicien in this.NomDemandeAide) {
            if(DAL.getUserByUsername(Environment.UserName).fullName.Equals(nomTechnicien))
                return true;
        }
        return false;*/

        return this.NomDemandeAide.Contains(DAL.getUserByUsername(Environment.UserName).fullName);
    }

    /// <summary>
    /// Fonction qui permet de retirer un technicien de la liste des techniciens sollicités pour de l'aide.     
    /// </summary>
    public void removeFromHelp()
    {
        this.NomDemandeAide.Remove(DAL.getUserByUsername(Environment.UserName).fullName);
    }

    public string interlocuteurHDInitiale
    {
        get
        {
            string ret = "";
            foreach (char c in this.interlocuteurHD.ToCharArray())
            {
                if (char.IsUpper(c))
                    ret += c;
            }
            return ret;
        }
    }

    public string techniciensString
    {
        get
        {
            string ret = "";
            string[] ss = this.Techniciens.Split(';');
            for (int i = 0; i < ss.Length - 1; i++)
            {
                if (i == 0)
                    ret = ss[i];
                else if (i == ss.Length - 2)
                    ret += " et " + ss[i];
                else
                    ret += ", " + ss[i];
            }
            return ret;
        }
    }

    public string NumSuivi
    {
        get
        {
            string ret = "";
            if (id < 1000000)
            {
                ret += "0";
                if (id < 100000)
                {
                    ret += "0";
                    if (id < 10000)
                    {
                        ret += "0";
                        if (id < 1000)
                        {
                            ret += "0";
                            if (id < 100)
                            {
                                ret += "0";
                                if (id < 10)
                                    ret += "0";
                            }
                        }
                    }
                }
            }
            return ret + id.ToString();
        }
    }
}

【问题讨论】:

  • 你试过link这个解决方案吗?
  • 我试过没有结果:/
  • 哪一行抛出实体异常?看来您的代码不完整,请提供更多有关实体结构的详细信息以及该方法应执行的操作。
  • 能否提供FichePreDiag实体类及其映射关系?
  • 使用 EF 6.1.3 并非如此。您是否使用 Code First 方法?您是否仔细检查了模型是否与您在 db 表中的完全匹配?似乎有些东西不像以前那样映射了。

标签: c# database


【解决方案1】:

好吧,我知道问题出在哪里了,天哪,我很愚蠢 xD

我更改了代码中的功能,所以我必须将“nomDemandeAide”(字符串)更改为列表...所以我只需将其更改回字符串,与 DateTimeDemandeAide 相同

谢谢大家的帮助:)

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 2013-11-10
    • 2014-11-30
    • 2017-06-19
    • 2011-06-26
    • 2014-01-08
    • 2014-03-29
    • 2018-02-16
    相关资源
    最近更新 更多