私はASP.NETに全く新しいですが、これはASP.NET Coreの私の最初のアプリです。移行を作成した後にデータベースを更新する際に問題があります。私はコマンドを入力している間: dotnet ef database update
、私はエラーが発生します:
各表の列名は一意でなければなりません。テーブル 'Adverts'の列名 'PortalUserId'が複数指定されています。
問題は私のモデル構造にあると思いますが、私は何が間違っているのか分かりません。私がASP.NET MVC 5で開発していたとき、すべてがOKでした。
ここに私のモデルがあります(ケースのエンティティには不要です):
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; }
}
私がここでやっていることは、遅延読み込みのための通常の仮想マッピングです。私は、PortalUser in AdvertフィールドにFKを保存しています。
私はすべての役に立つ答えを感謝します!
私はすでに怠惰な読み込みがサポートされていないので、私のモデルは公式のチュートリアルのように見えます。
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; }
}
さて、みんな、私は解決策を見つけた!必要なものはすべて、intからstringへのプロパティPortalUserIdのタイプの変更です。すべてがコンパイルされ、倍増フィールドは現れません!
少なくとも私を助けてくれてありがとう!