I am quite new to ASP.NET at all, however this is my first app with ASP.NET Core. I have problem with updating database after creating the migration. While I type command: dotnet ef database update
, I get error:
Column names in each table must be unique. Column name 'PortalUserId' in table 'Adverts' is specified more than once.
I think the problem is with my model structure, but I do not know what I am doing wrong. When I was developing with ASP.NET MVC 5 everything was Ok.
Here is my Model (without unnecessary for the case entities):
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<PortalUser> PortalUsers { get; set; }
public DbSet<Advert> Adverts { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
public class Advert
{
public int ID { get; set; }
public string Title { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
public int PortalUserID { get; set; }
public PortalUser PortalUser { get; set; }
}
public class PortalUser : IdentityUser
{
public string FirstName { get; set; }
public string Surname { get; set; }
public ICollection<Advert> Adverts { get; set; }
}
What I am doing here is normal virtual mapping for lazy loading purposes. I am storing FK to PortalUser in Advert field.
I will appreciate every helpful answer!
I already figure out, that lazy loading is not supported so now my model looks like in the official tutorial:
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Okay, guys, I found the solution! All what is needed, it is change type of property PortalUserId from int to string. Than everythings compiles and no doubled field appears!
Thanks for at least trying to help me!