Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ERROR
- The seed entity for entity type 'User' cannot be added because it has the navigation 'Person' set. To seed relationships, add the entity seed to 'User' and specify the foreign key values {'PersonId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.
- */
- public class UserTypeConfiguration : IEntityTypeConfiguration<User>
- {
- public void Configure(EntityTypeBuilder<User> builder)
- {
- builder.HasKey(u => u.PersonId);
- builder.HasOne(u => u.Person).WithOne(p => p.User).HasForeignKey<User>(u => u.PersonId);
- builder.HasIndex(p => p.Email).IsUnique();
- builder.HasIndex(u => u.Login).IsUnique();
- builder.HasData(new User
- {
- Email = "email@email.com",
- Login = "admin",
- Password = "123",
- PersonId = 1,
- });
- }
- }
- public class PersonTypeConfiguration : IEntityTypeConfiguration<Person>
- {
- public void Configure(EntityTypeBuilder<Person> builder)
- {
- builder.HasKey(p => p.Id);
- builder.HasData(new Person
- {
- Name = "administrator",
- Birth = DateTime.Today,
- Id = 1
- });
- }
- }
- public class DataBaseContext : DbContext, IDataBaseContext
- {
- public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options)
- {
- }
- public DbSet<Person> People { get; set; }
- public DbSet<User> Users { get; set; }
- protected override void OnModelCreating(ModelBuilder builder)
- {
- //User
- builder.ApplyConfiguration(new PersonTypeConfiguration());
- builder.ApplyConfiguration(new UserTypeConfiguration());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement