“每个表中的列名必须是唯一的。”在数据库更新期间 [英] "Column names in each table must be unique." during database update
问题描述
我对ASP.NET完全陌生,但这是我的第一个带有ASP.NET Core的应用程序。创建迁移后,我在更新数据库时遇到问题。当我键入命令: dotnet ef数据库更新
时,出现错误:
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每个表中的名称必须唯一。表 Adverts中的列名 PortalUserId
被多次指定。
Column names in each table must be unique. Column name 'PortalUserId' in table 'Adverts' is specified more than once.
我认为问题出在我的模型结构,但我不知道我在做什么错。当我使用ASP.NET MVC 5进行开发时,一切都很好。
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; }
}
我在这里所做的是懒惰的普通虚拟映射加载目的。我正在将FK存储到广告字段中的PortalUser。
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/zh-cn/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。比一切都编译,并且没有出现双倍字段!
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!
至少感谢您的帮助!
这篇关于“每个表中的列名必须是唯一的。”在数据库更新期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!