ASP.NET / Entity Framework - The entity type PartenaireResult is not part of the model for the current context

I'm new to ASP.NET and can't manage to find any solution nor related thread to my issue.

I want to update an user in database with a onesignal unique identifier via an action method that is getting the current connected User.

I am using database first approach.

I get the following error when launching the request:

System.InvalidOperationException: The entity type PartenaireResult is not part of the model for the current context.

The thing is that PartenaireResult is not an entity on my table, only a class for returning datas.

I tried this solution with no luck (only adding another connection string named "DefaultConnection", but I don't understand the concept of NinjectWebCommons.

I also tried to put this coe in my DbContext class with no luck as stated in this thread:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<PartenaireResult>().ToTable("PartenaireResult");
}

Here is the non working method:

[Authorize(Roles = "partenaire")]
[Route("api/Partenaires/Me")]
public PartenaireResult GetClientsMe(string onesignal_id)
{
    var connectedUser = GetConnectedUser(User);
    connectedUser.onesignal_id = onesignal_id;

    db.Entry(connectedUser).State = System.Data.Entity.EntityState.Modified;
    db.SaveChanges();

    return connectedUser;
}

[Authorize(Roles = "partenaire")]
public static PartenaireResult GetConnectedUser(IPrincipal user)
{
    var claimIdentity = user.Identity as ClaimsIdentity;
    var claim = claimIdentity?.Claims?.FirstOrDefault(x => x.Type.Equals("IdClient", StringComparison.InvariantCultureIgnoreCase));

    if (claim != null)
    {
        int idPartenaire;

        if (int.TryParse(claim.Value, out idPartenaire))
        {
            using (var db = new UphairDbEntities())
            {
                var me = db.Partenaires.Find(idPartenaire);

                PartenaireResult PartenaireRes = new PartenaireResult();
                ObjectConverterHelper.CopyProperties(me, PartenaireRes);

                return PartenaireRes;
            }
        }
    }

    return null;
}

Here's my PartenaireResult class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Web;
using Uphair.EfModel;

namespace Uphair.Api.Models.Partenaire
{
    public class PartenaireResult
    {
        public PartenaireResult()
        {
        }

        public int IdPartenaire { get; set; }
        public string NomComplet { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string PasswordSalt { get; set; }
        public PartenaireType Type { get; set; }
        public string Pays { get; set; }
        public string Ville { get; set; }
        public string CodePostale { get; set; }
        public string Adresse { get; set; }
        public Nullable<double> Lat { get; set; }
        public Nullable<double> Lng { get; set; }
        public string ImageUrl { get; set; }
        public string CouvertureUrl { get; set; }
        public string TelMobile { get; set; }
        public Nullable<System.DateTime> DateNaissance { get; set; }
        public bool ADomicile { get; set; }
        public int SeDeplace { get; set; }

        public string IdWallet { get; set; }
        public string IdUserMango { get; set; }
        public Nullable<System.DateTime> DateAjout { get; set; }

        public string onesignal_id { get; set; }

        public List<NoteItem> Notes { get; set; }

        /**/
        public double NoteGlobale { get; set; }

        public bool Son { get; set; }
        public bool Push { get; set; }
        public string IdPhone { get; set; }
    }

    public class NoteItem
    {
        public int? IdClient { get; set; }
        public string Commentaire { get; set; }
        public string ImageUrl { get; set; }
        public double Note { get; set; }
        public DateTime? DateAjout { get; set; }

        public int Valide { get; set; }
    }
}

Any kind of welp would be appreciated.

Thank to anyone who will take the time to read/answer this post.

1 answer

  • answered 2018-10-11 20:27 Cygnus

    The root cause of this problem is the DbContext (the "UphairDbEntities" instance) doesn't have the 'PartenaireResult' type.

    1. First, I would de-collapse the .edmx file in your project and find the "....tt" file and expand it. You should see a "..cs" file for every table that you included in your edmx file.
    2. Make sure there is a "PartenaireResult.cs" file there

    If the file is not there, one of two things is happening:

    1. You have a second .edmx file (maybe that you deleted before creating this one) that already has a "PartenaireResult.cs" file. Delete that file (if it exists), open up and re-save your .edmx file to regenerate the "PartenaireResult.cs" file as part of your project.
    2. Your PartenaireResult.cs is not part of the .edmx, so your UphairDbEntities context doesn't have a PartenaireResult type.

    This problem can frequently happen when you're using multiple contexts that share a table. If Entities1() and Entities2() both reference the same table 'Table1', then only one of those Contexts will have the table. One workaround for this issue is to rename the mapped tablename in one .edmx; the second workaround is to only keep the table in one context.