Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Problem with creating migration ASP.NET Core, EF Core.

$
0
0

Hello. 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 what I am doing wrong. When I was developing in 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 virtual PortalUser PortalUser { get; set; }
    }

public class PortalUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public virtual 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!

Kind Regards,

Pawel


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>