无法找到对象,因为它不存在,或者您在更新数据库时没有权限

时间:2019-10-11 09:23:46

标签: c# asp.net-web-api ef-core-2.2

迁移成功,但是当尝试更新数据库时,出现该错误。 我之前也遇到过类似的问题,因为我没有为表创建适当的DbSet<>。这次不是事实了。

我的DBContext:

using System;
using System.Collections.Generic;
using System.Text;
using ArtGalleries.Domain;
using Microsoft.EntityFrameworkCore;

namespace ArtGalleries.Data
{
    public class ArtGalleriesContext:DbContext
    {
        public ArtGalleriesContext(DbContextOptions<ArtGalleriesContext> options):base(options)
        {

        }

        public DbSet<ArtItem> ArtItems { get; set; }
        public DbSet<Artist> Artists { get; set; }
        public DbSet<Company> Companies { get; set; }
        public DbSet<Floor> Floors { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<User> Users { get; set; }






        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserLocation>()
                .HasKey(u => new {u.UserId, u.LocationId});
            //modelBuilder.Entity().
            //modelBuilder.Entity<Company>()
            //    .HasOne(c => c.Company)
            //    .WithOne(pc => pc.Company)
            //    .HasForeignKey(fk => fk.ParentId);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(
                    "Server=(localdb)\\mssqllocaldb;Database=GalleriesDB;Trusted_Connection=True;");
            }
        }
    }
}

我的问题课:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace ArtGalleries.Domain
{
    public class ArtItem
    {
        public string ArtType { get; set; }
        public Artist Artist { get; set; }
        public int ArtistId { get; set; }
        public int Depth { get; set; }
        public string Description { get; set; }
        public string Framing { get; set; }
        public int Height { get; set; }
        [Required]
        public int Id { get; set; }
        public Location Location { get; set; }
        [Required]
        public int LocationId { get; set; }
        public string Materials { get; set; }
        [Required]
        public string Name { get; set; }
        public User Owner { get; set; }
        public int OwnerId { get; set; }
        public string Picture { get; set; }
        public Room Room { get; set; }
        public int RoomId { get; set; }
        public string Technique { get; set; }
        public int Width { get; set; }
        public int YearOfAcquisition { get; set; }
        public int YearOfCreation { get; set; }
        public Status Status { get; set; }
    }
}

我不熟悉权限。我注意到了一个类似的问题,建议该人员查找Up()和Down()方法并与之一起使用。我没有这样的文件。可能是因为它们的问题是由EF而不是EF Core引起的。 类似的问题标题:“找不到对象dbo.xxxx,因为它不存在或您没有权限。”

1 个答案:

答案 0 :(得分:0)

此问题已解决,方法是删除迁移并添加新的迁移,而无需对代码进行任何编辑。很奇怪。