Ошибка заполнения таблицы данных с использованием EF Core MVC - PullRequest
0 голосов
/ 09 января 2020

Я новичок в MVC и EF Core и пытаюсь заполнить некоторые данные. Все мои столы засеяны правильно, кроме одного стола, и я устала биться головой о стену. Я даже воссоздал все приложение без удачи. Я уверен, что упускаю что-то очевидное.

Мои модели

public class AgreementType
    {
        public int AgreementTypeID { get; set; }

        [Required]
        [StringLength(100)]
        public string Description { get; set; }

        [DisplayName("Creation Date")]
        public DateTime CreationDate { get; set; }

        [StringLength(100)]
        [DisplayName("Created By")]
        public string CreatedBy { get; set; }

        public Agreement Agreement { get; set; }
    }

public class ContactType
    {
        public int ContactTypeID { get; set; }

        [Required]
        [StringLength(100)]
        public string Description { get; set; }

        [DisplayName("Creation Date")]
        public DateTime CreationDate { get; set; }

        [StringLength(100)]
        [DisplayName("Created By")]
        public string CreatedBy { get; set; }

        public Contact Contact { get; set; }
    }

public class Branch
    {
        public int BranchID { get; set; }
        public string Name { get; set; }

        [DisplayName("Creation Date")]
        public DateTime CreationDate { get; set; }

        [StringLength(100)]
        [DisplayName("Created By")]
        public string CreatedBy { get; set; }

        public int IntermediaryID { get; set; }
        public Intermediary Intermediary { get; set; }

        public int RegionID { get; set; }
        public virtual Region Region { get; set; }
    }

Мой контекстный класс

public class BizDevHubContext : DbContext
    {
        public BizDevHubContext(DbContextOptions<BizDevHubContext> options) : base(options)
        {
        }

        public DbSet<AgreementType> AgreementType { get; set; }
        public DbSet<Branch> Branch { get; set; }
        public DbSet<ContactType> ContactTypes { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AgreementType>().ToTable("AgreementType");
            modelBuilder.Entity<Branch>().ToTable("Branch");
            modelBuilder.Entity<ContactType>().ToTable("ContactType");

            // One to One            
            modelBuilder.Entity<Agreement>()
                .HasOne(b => b.AgreementType)
                .WithOne(i => i.Agreement)
                .HasForeignKey<Agreement>(b => b.AgreementTypeID);

            modelBuilder.Entity<Contact>()
                .HasOne(b => b.ContactType)
                .WithOne(i => i.Contact)
                .HasForeignKey<Contact>(b => b.ContactTypeID);

            //One to Many
            modelBuilder.Entity<Region>()
                .HasMany(c => c.Branch)
                .WithOne(e => e.Region)
                .HasForeignKey((p => p.BranchID));

            // Set default dates 
            modelBuilder.Entity<AgreementType>()
            .Property(b => b.CreationDate)
            .HasDefaultValueSql("getdate()");

            modelBuilder.Entity<Branch>()
            .Property(b => b.CreationDate)
            .HasDefaultValueSql("getdate()");

            modelBuilder.Entity<ContactType>()
            .Property(b => b.CreationDate)
            .HasDefaultValueSql("getdate()");
        }
    }

Мой класс инициализатора

    if (context.Branch.Any())
                {
                    return;
                }

                var branches = new Branch[]
                {
                 new Branch{Name="Bloemfontein",CreationDate=DateTime.Now,CreatedBy="System"},
                 new Branch{Name="Cape Town",CreationDate=DateTime.Now,CreatedBy="System"},
                 new Branch{Name="Durban",CreationDate=DateTime.Now,CreatedBy="System"},
                 new Branch{Name="Nelspruit",CreationDate=DateTime.Now,CreatedBy="System"},
                 new Branch{Name="Johannesburg",CreationDate=DateTime.Now,CreatedBy="System"},
                 new Branch{Name="Port Shepstone",CreationDate=DateTime.Now,CreatedBy="System"},
                 new Branch{Name="Pretoria",CreationDate=DateTime.Now,CreatedBy="System"}
                };
                foreach (Branch b in branches)
                {
                    context.Branch.Add(b);

                }
                context.SaveChanges();

В других таблицах все начальные числа, кроме таблицы ветвей.

Пожалуйста, помогите!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...