C
C#3y ago
esther

How to migrate table?

I have database with table: Quote, so i tried to migrate new table Author on database using package manager console but there's an error: System.InvalidOperationException: 'Cannot use table 'Quote' for entity type 'Sample' since it is being used for entity type 'Author' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'Sample' on the primary key properties and pointing to the primary key on another entity typed mapped to 'Quote'.' Here my Author.cs model:
namespace Entities.Models
{
public class Author : BaseEntity
{
[Sieve(CanFilter = true, CanSort = true)]
public string Name { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
public string Email { get; set; }
public ICollection<Sample> Quote { get; set; } //relation table
}
}
namespace Entities.Models
{
public class Author : BaseEntity
{
[Sieve(CanFilter = true, CanSort = true)]
public string Name { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
public string Email { get; set; }
public ICollection<Sample> Quote { get; set; } //relation table
}
}
Here my Sample.cs model:
namespace Entities.Models
{
[Table("Quote")]
public class Sample : BaseEntity
{
[Column("Quote")]
[Sieve(CanFilter = true, CanSort = true)]
public string Quote { get; set; }

[ForeignKey(nameof(Author))]
public Guid? AuthorId { get; set; }
public Author Author { get; set; }
}
}
namespace Entities.Models
{
[Table("Quote")]
public class Sample : BaseEntity
{
[Column("Quote")]
[Sieve(CanFilter = true, CanSort = true)]
public string Quote { get; set; }

[ForeignKey(nameof(Author))]
public Guid? AuthorId { get; set; }
public Author Author { get; set; }
}
}
and here RepositoryContext.cs:
namespace Entities
{
public class RepositoryContext : DbContext
{
public RepositoryContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(RepositoryContext).Assembly);
}

public DbSet<Sample> Samples { get; set; }
public DbSet<Author> Author { get; set; }
}
}
namespace Entities
{
public class RepositoryContext : DbContext
{
public RepositoryContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(RepositoryContext).Assembly);
}

public DbSet<Sample> Samples { get; set; }
public DbSet<Author> Author { get; set; }
}
}
3 Replies
esther
estherOP3y ago
@Angius can you help me?
Angius
Angius3y ago
Why is it called "Sample" if it's a quote..? Seems like you were trying to do some other stuff before, tables had different names and what not, and EF is confused now. Dropping the database, deleting the migrations, and creating a fresh migration often helps.
esther
estherOP3y ago
I thought that when declare the [Table: Quote] will help, okay i will try to edit that and create fresh migration. Thanks:)
Want results from more Discord servers?
Add your server