找不到 object,因为它不存在或您在更新数据库时没有权限
Cannot find the object because it does not exist or you do not have permissions when updating database
迁移成功,但在尝试更新数据库时,出现该错误。
我之前遇到过类似的问题,因为我没有为 table 创建合适的 DbSet<>
。这一次,不是这样。
我的数据库上下文:
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;");
}
}
}
}
我的问题class:
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 生成的。
类似问题标题:“找不到 object”dbo.xxxx“因为它不存在或您没有权限。”
这已通过删除迁移并添加新迁移解决,未对代码进行任何编辑。奇怪。
迁移成功,但在尝试更新数据库时,出现该错误。
我之前遇到过类似的问题,因为我没有为 table 创建合适的 DbSet<>
。这一次,不是这样。
我的数据库上下文:
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;");
}
}
}
}
我的问题class:
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 生成的。 类似问题标题:“找不到 object”dbo.xxxx“因为它不存在或您没有权限。”
这已通过删除迁移并添加新迁移解决,未对代码进行任何编辑。奇怪。