尝试使用 EF6 Code First 播种数据时出错

Getting error trying to seed data using EF6 Code First

模型看起来像这样

    public class Account : BaseEntity
{
    public string AccountNumber { get; set; }
    public string AccountTitle { get; set; }
    public decimal CurrentBalance { get; set; }
    public AccountStatus AccountStatus { get; set; }
    [ForeignKey("UserId")]
    public string UserId { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Transaction> Transactions { get; set; }
}
public enum AccountStatus
{
    Active = 0,
    InActive = 1
}

    public class User : BaseEntity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string ProfilePicUrl { get; set; }
    public virtual Account Account { get; set; }
}
public class Transaction : BaseEntity
{
    public TransactionType TransactionType { get; set; }
    public DateTime TransactionDate { get; set; }
    public decimal TransactionAmount { get; set; }
    public virtual Account Account { get; set; }
}
public enum TransactionType
{
    Deposit = 0,
    Withdraw = 1
}

所以从技术上讲,用户有一个账户,账户有一个或多个交易

我正在播种如下数据 基本上有 3 个用户,其中 2 个有关联的帐户

  public class BBBankContext : DbContext
{
    public BBBankContext(DbContextOptions<BBBankContext> options) : base(options) { }
    public DbSet<User> Users { get; set; }
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Transaction> Transactions { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
       // modelBuilder.Entity<User>()
       //.HasOne<Account>(s => s.Account)
       //.WithOne(ad => ad.User)
       //.HasForeignKey<Account>(ad => ad.UserId);


        modelBuilder.Entity<User>(b =>
        {
            b.HasData(new User
            {
                Id = "2a2615d0-1c0d-4b9c-b41b-cc01aeb35919",
                FirstName = "Raas",
                LastName = "Masood",
                Email = "admin@patternstech.com",
                ProfilePicUrl = "",

            });

            b.HasData(new User
            {
                Id = "b6111852-a1e8-4757-9820-70b8c20e1ff0",
                FirstName = "Ali",
                LastName = "Taj",
                Email = "malitaj-dev@outlook.com",
                ProfilePicUrl = ""

            });
            b.HasData(new User
            {
                Id = "582ebb0b-f9e0-4385-8787-37bd337f18b7",
                FirstName = "Salman",
                LastName = "Taj",
                Email = "salman-dev@outlook.com",
                ProfilePicUrl = ""

            });
        });
        modelBuilder.Entity<Account>(b =>
        {
            b.HasData(new Account
            {
                Id = "37846734-172e-4149-8cec-6f43d1eb3f60",
                AccountNumber = "0001-1001",
                AccountTitle = "Ali Taj",
                CurrentBalance = 2055M,
                AccountStatus = AccountStatus.Active,
                UserId = "b6111852-a1e8-4757-9820 -70b8c20e1ff0"

            });

            b.HasData(new Account
            {
                Id = "2f115781-c0d2-4f98-a70b-0bc4ed01d780",
                AccountNumber = "0002-2002",
                AccountTitle = "Salman Taj",
                CurrentBalance = 545M,
                AccountStatus = AccountStatus.Active,
                UserId = "582ebb0b-f9e0-4385-8787-37bd337f18b7"

            });
        });
        modelBuilder.Entity<Transaction>().HasData(
              new
              {
                  Id = Guid.NewGuid().ToString(),
                  AccountId = "37846734-172e-4149-8cec-6f43d1eb3f60",
                  TransactionAmount = 1000M,
                  TransactionDate = DateTime.Now,
                  TransactionType = TransactionType.Deposit
              },
              new
               .....

添加迁移和 运行“更新数据库”后出现错误

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Accounts_Users_UserId". The conflict occurred in database "BBBankDB", table "dbo.Users", column 'Id'.

花了好几个小时才弄明白:(我做错了什么?

我认为问题是你在这个种子中 UserId 有误。

    b.HasData(new Account
    {
        Id = "37846734-172e-4149-8cec-6f43d1eb3f60",
        AccountNumber = "0001-1001",
        AccountTitle = "Ali Taj",
        CurrentBalance = 2055M,
        AccountStatus = AccountStatus.Active,
        UserId = "b6111852-a1e8-4757-9820 -70b8c20e1ff0"
    //                                    ^
    //                                   Here
    });

删除这个额外的 space,您的错误就会消失。

UserId = "b6111852-a1e8-4757-9820-70b8c20e1ff0"