储存库EF CF无法引用不存在的字段

时间:2018-12-26 13:03:53

标签: c# asp.net-core ef-code-first entity-framework-core

我正在使用存储库模式和.NET Core的代码优先构建数据访问层。当它只是普通的.NET Framework时,我对Code First没问题,但是.NET Core存在很多问题。

当前的是:我有一堆简单的类,并且我试图获取表中存储的内容的列表,并且得到一条奇怪的错误消息: 无效的列名“ AspNetUserId”。但是我没有这样的列名。我有一个表AspNetUsers,它有一个字段ID。该表引用的其他表将UserId字段用作FK。

下面是主要类的简化代码:

CREATE TABLE [dbo].[AspNetUsers](
    [Id] [uniqueidentifier] NOT NULL,
...
 CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH ...


CREATE TABLE [dbo].[Accounts](
    [AccountAddress] [nvarchar](150) NOT NULL,
    [UserId] [uniqueidentifier] NOT NULL,
...
 CONSTRAINT [PK_Accounts_1] PRIMARY KEY CLUSTERED 
(
    [AccountAddress] ASC
)WITH...


    public class AspNetUser
    {
        [Key]        
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid Id { get; set; } // Id (Primary key)

      ...
  }

 public class DataContext : DbContext   
 {
        public DbSet<Account> Accounts { get; set; }

        public DbSet<AspNetUser> AspNetUsers { get; set; }
        ...

 }

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
            modelBuilder.Entity<AspNetUser>()
                .HasMany(p => p.Accounts)
                .WithOne(p => p.AspNetUser);

            modelBuilder.Entity<AspNetUser>()
                .HasOne(p => p.Currency);

            modelBuilder.Entity<Account>()
                .HasOne(p => p.Currency);

          ...


            base.OnModelCreating(modelBuilder);
 }

0 个答案:

没有答案