Home > Mobile >  Getting error trying to seed data using EF6 Code First
Getting error trying to seed data using EF6 Code First

Time:04-10

The models looks like this

    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
}

So technically User has One account and Account have one or many Transactions

i am seeding data like below Basically there are 3 users, 2 of them have accounts associated with them

  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 = "[email protected]",
                ProfilePicUrl = "",

            });

            b.HasData(new User
            {
                Id = "b6111852-a1e8-4757-9820-70b8c20e1ff0",
                FirstName = "Ali",
                LastName = "Taj",
                Email = "[email protected]",
                ProfilePicUrl = ""

            });
            b.HasData(new User
            {
                Id = "582ebb0b-f9e0-4385-8787-37bd337f18b7",
                FirstName = "Salman",
                LastName = "Taj",
                Email = "[email protected]",
                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
               .....

after adding migration and running "Update-Database" i am getting error

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

Spent many hours tyring to figure this out :( what am i doing wrong ?

CodePudding user response:

I think the problem is you have a wrong UserId in this seed.

    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
    });

Remove this additional space and your error will be gone.

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