Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
The entity type is not part of the model error
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Titre:
The entity type is not part of the model error
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01613695
Message ID:
01613695
Vues:
38
UPDATE 2. Got it working after I re-generated all the files using Custom Tool.

Update. More information and questions here

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/f588fe71-c670-458b-bde2-13681369a4b2/using-sql-server-views-with-code-first-ef-6?forum=adodotnetentityframework

Hi everybody,

I am getting this error "The entity type GuestsList is not part of the model for the current context." after I tried to add a view.

We're using reverse POCO generator in the project. It automatically creates models and configurations. In the Solution Explorer it looks like this:

SiriusSql.tt and underneath all the configuration files in the Siriusware.Data project. This project also has SiriusSqlContext.Views.tt with 2 files underneath - SiriusSqlContext.Views.cs and SiriusSqlContext.Views.xml. These files are automatically generated.

We also have Siriusware.Models project that has SiriusSQL.tt file with all the models based on database tables underneath.

Now, in the Siriusware.Models project we also have Partials subfolder with some extra partial classes, in particular, this class:
public partial class GuestsList
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [Column(TypeName = "varchar")]
        public string Phone { get; set; }
        public decimal GuestNo { get; set; }
        public decimal ParentNo { get; set; }
        public DateTime? BirthDate { get; set; }
        public int? Age { get; set; }
    }
The cs file with this class is named Guests.cs, I don't know if this is important.

Under Siriusware.Data I added the following configuration file (directly in the Data project, not underneath SiriusSQLContext.tt):
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using Siriusware.Models;
namespace Siriusware.Data
{
    // siriusv_GuestsList
    internal partial class GuestsListConfiguration : EntityTypeConfiguration<GuestsList>
    {
        public GuestsListConfiguration(string schema = "dbo")
        {
            ToTable(schema + ".siriusv_GuestsList");
            HasKey(x => x.GuestNo);

            Property(x => x.GuestNo).HasColumnName("GuestNo").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            Property(x => x.ParentNo).HasColumnName("ParentNo");           
            Property(x => x.FirstName).HasColumnName("FirstName").IsUnicode(false).HasMaxLength(100);
            Property(x => x.LastName).HasColumnName("LastName").IsUnicode(false).HasMaxLength(100);
            Property(x => x.Phone).HasColumnName("Phone").IsUnicode(false).HasMaxLength(50);
            Property(x => x.Age).HasColumnName("Age").IsOptional();
            Property(x => x.BirthDate).HasColumnName("BirthDate").IsOptional();
           
            InitializePartial();
        }
        partial void InitializePartial();
    }
}
This is very similar to what we have for other configurations which are auto-generated (for the normal tables, not view).

Now, in the GuestsRepository I have the following code
public PagedResult<GuestsList> GetPagedGuestListRequest(SearchRequest searchRequest)
        {
            IDbSet<GuestsList> dbSet = _siriusContext.Set<GuestsList>(); 
            var query = SearchHelper.GetFilteredSearch<GuestsList>(dbSet.AsQueryable(), searchRequest);
            var pagedResultMessage = SearchHelper.GetPagedResult(query, searchRequest);
            return pagedResultMessage;        
        }
This is practically the same as we have for other "table-based" classes. The third line here generates the error in the title of the thread.

I am not sure what else do I need to do and how exactly I am supposed to let my context know to use this view. E.g. in addition to the automatically generated (by Reversed POCO generator) I want to be able to use some extra views.

Do you know what is missing here and are my steps correct or not so far?

Thanks a lot in advance.
If it's not broken, fix it until it is.


My Blog
Répondre
Fil
Voir

Click here to load this message in the networking platform