Error Invalid object name attempt to connect to a non-existent database table

0

I’m doing a diplom project on the Acp.Net core mvc 5 platform. I created a role provider. I try in the method public override string [] GetRolesForUser (string username) to get user roles, here is the method itself

 public override string[] GetRolesForUser(string username)
        {
            string[] roles = new string[] { };
            using (ITSMContext db = new ITSMContext())
            {
                Specialist specialist = db.Specialists.FirstOrDefault(s => s.Login == username);
                //int a = specialist.Id;
                if (specialist != null)
                {

                    var allRoles = db.ListSpecialistRoles.Where(r => r.SpecialistId == specialist.Id)
                        .FirstOrDefault();
                    ListSpecialistRole specialistRole = db.ListSpecialistRoles.Find(allRoles.SpecialistId);
                    Role role = db.Roles.FirstOrDefault(r => r.Id == specialistRole.RolesId);
                    if (specialistRole != null)
                        roles = new string[] { role.NameRole };
                }
            }
            return roles;
        }

When executing, I get the following error

Invalid object name 'dbo.ListSpecialistRoles1'.
Описание: Необработанное исключение при выполнении текущего веб-запроса. Изучите трассировку стека для получения дополнительных сведений о данной ошибке и о вызвавшем ее фрагменте кода.

Сведения об исключении: System.Data.SqlClient.SqlException: Invalid object name 'dbo.ListSpecialistRoles1'.

Ошибка источника:


Строка 58:                 {
Строка 59:                     
Строка 60:                     var allRoles = db.ListSpecialistRoles.Where(r => r.SpecialistId == specialist.Id)
Строка 61:                         .FirstOrDefault();
Строка 62:                     ListSpecialistRole specialistRole = db.ListSpecialistRoles.Find(allRoles.SpecialistId);

Исходный файл: E:\Артём\VISUAL\ITSMForSchool\ITSMForSchool\Providers\CustomRoleProvider.cs    Строка: 60

Трассировка стека:


[SqlException (0x80131904): Invalid object name 'dbo.ListSpecialistRoles1'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2573710
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +6014662
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +297
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4291
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +59

For some reason, the program is trying to create a table dbo.ListSpecialistRoles1. Role and Specialist tables are linked many-to-many by the ListSpecialistRoles table. In the context of the data, I registered these relationships

   public class ITSMContext : DbContext
    {
        public DbSet<Position> Positions { get; set; }
        public DbSet<Specialist> Specialists { get; set; }
        public DbSet<Request> Requests { get; set; }
        public DbSet<RequestProcessing> RequestProcessings { get; set; }
        public DbSet<Status> Statuses { get; set; }
        public DbSet<RequestType> RequestTypes { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<TypeAsset> TypesAsset { get; set; }
        public DbSet<Asset> Assets { get; set; }
        public DbSet<ListSpecialistRole> ListSpecialistRoles { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Role>().HasMany(c => c.Specialists)
                .WithMany(s => s.Roles)
                .Map(t => t.MapLeftKey("RolesId")
                .MapRightKey("SpecialistId")
                .ToTable("ListSpecialistRoles"));

            modelBuilder.Entity<Specialist>().HasMany(c => c.Roles)
                .WithMany(s => s.Specialists)
                .Map(t => t.MapLeftKey("SpecialistId")
                .MapRightKey("RolesId")
                .ToTable("ListSpecialistRoles"));

Please help me figure out what the error is.

asp.net-mvc-5
entity-framework-core

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0