“数据库更新期间,每个表中的列名必须是唯一的。”

问题描述 投票:2回答:1

我根本不熟悉ASP.NET,但这是我第一个使用ASP.NET Core的应用程序。创建迁移后,我在更新数据库时遇到问题。当我输入命令:dotnet ef database update时,我收到错误:

每个表中的列名必须是唯一的。表'广告'中的列名'PortalUserId'被多次指定。

我认为问题在于我的模型结构,但我不知道我做错了什么。当我使用ASP.NET MVC 5进行开发时,一切都很好。

这是我的模型(对案例实体没有必要):

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; }

}

我在这里做的是用于延迟加载目的的普通虚拟映射。我将FK存储到广告字段中的PortalUser。

我将感谢每一个有用的答案!

我已经知道了,不支持延迟加载所以现在我的模型在官方教程中看起来像:

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; }
}
c# entity-framework ef-code-first asp.net-core entity-framework-core
1个回答
1
投票

好的,伙计们,我找到了解决方案!所需的一切,它是从int到string的属性PortalUserId的更改类型。比所有事情都要编译,并且没有出现加倍的字段!

谢谢你至少试着帮助我!

© www.soinside.com 2019 - 2024. All rights reserved.