gpt4 book ai didi

c# - "Column names in each table must be unique."数据库更新期间

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:38 26 4
gpt4 key购买 nike

我对 ASP.NET 完全陌生,但这是我使用 ASP.NET Core 的第一个应用程序。创建迁移后更新数据库时遇到问题。当我键入命令:dotnet ef database update 时,出现错误:

Column names in each table must be unique. Column name 'PortalUserId' in table 'Adverts' is specified more than once.

我认为问题出在我的模型结构上,但我不知道我做错了什么。当我使用 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 存储到 Advert 字段中的 PortalUser。

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

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

https://learn.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; }
}

最佳答案

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

至少感谢你试图帮助我!

关于c# - "Column names in each table must be unique."数据库更新期间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40702704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com