Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to seed in navigation property?

$
0
0

I have several models in this Db, 

I am using the following seeding class,

public class SeedData
    {
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new ArtCoreDbContext(
                serviceProvider.GetRequiredService<DbContextOptions<ArtCoreDbContext>>()))
            {
                // Look for any movies.
                if (context.Genders.Any())
                {
                    return;   // DB has been seeded
                }

                context.Genders.AddRange(
                     new Genders
                     {
                         Name = "Male"
                     },

                     new Genders
                     {
                         Name = "Female"
                     }

                );
                context.SaveChanges();

                if (context.Statuses.Any())
                {
                    return;   // DB has been seeded
                }

                context.Statuses.AddRange(
                     new Statuses
                     {
                         Name = "Active"
                     },

                     new Statuses
                     {
                         Name = "Inactive"
                     },

                     new Statuses
                     {
                         Name = "Cancelled"
                     }


                );
                context.SaveChanges();

                if (context.PatientsRegistry.Any())
                {
                    return;   // DB has been seeded
                }

                context.PatientsRegistry.AddRange(
                     new PatientsRegistry
                     {
                        PatientFileId = 1234,
                        FirstName = "John",
                        SecondName = "M",
                        LastName = "Doe",
                        Gender = {Id = 5, Name="Male"},
                        Status = {Id = 2, Name="Inactive"}
                     }
                );
                context.SaveChanges();

            }
        }
    }


and there models are

public class Genders
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

public class Statuses
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }


[Table("PatientsRegistry")]
    public class PatientsRegistry
    {   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long RecordId { get; set; }
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long PatientFileId { get; set; }
        public string FirstName { get; set; }
        public string SecondName { get; set; }
        public string LastName { get; set; }
        public Genders Gender { get; set; }
        public Statuses Status { get; set; }
        public ICollection<PartnersRegistry> Partners { get; set; }

        public PatientsRegistry()
        {
            Partners = new Collection<PartnersRegistry>();
        }

    }

I Succeded in seeding both models (Genders & Statuses) but not PatientsRegistry!, is it the nav. prop?


Viewing all articles
Browse latest Browse all 9386

Trending Articles