Advertisement
Rodrigo_Moraes

EF Core help plz

Apr 9th, 2021 (edited)
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. /*  ERROR
  2. 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.
  3. */
  4.  
  5. public class UserTypeConfiguration : IEntityTypeConfiguration<User>
  6.     {
  7.         public void Configure(EntityTypeBuilder<User> builder)
  8.         {
  9.             builder.HasKey(u => u.PersonId);
  10.  
  11.             builder.HasOne(u => u.Person).WithOne(p => p.User).HasForeignKey<User>(u => u.PersonId);
  12.  
  13.             builder.HasIndex(p => p.Email).IsUnique();
  14.             builder.HasIndex(u => u.Login).IsUnique();          
  15.            
  16.             builder.HasData(new User
  17.             {
  18.                 Email = "email@email.com",
  19.                 Login = "admin",
  20.                 Password = "123",
  21.                 PersonId = 1,
  22.             });
  23.         }
  24.     }
  25.  
  26. public class PersonTypeConfiguration : IEntityTypeConfiguration<Person>
  27.     {
  28.         public void Configure(EntityTypeBuilder<Person> builder)
  29.         {
  30.             builder.HasKey(p => p.Id);
  31.  
  32.             builder.HasData(new Person
  33.             {
  34.                 Name = "administrator",
  35.                 Birth = DateTime.Today,
  36.                 Id = 1
  37.             });
  38.  
  39.         }
  40.     }
  41.  
  42.  public class DataBaseContext : DbContext, IDataBaseContext
  43.     {
  44.         public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options)
  45.         {
  46.         }
  47.  
  48.         public DbSet<Person> People { get; set; }
  49.  
  50.         public DbSet<User> Users { get; set; }
  51.  
  52.         protected override void OnModelCreating(ModelBuilder builder)
  53.         {
  54.  
  55.             //User
  56.             builder.ApplyConfiguration(new PersonTypeConfiguration());
  57.             builder.ApplyConfiguration(new UserTypeConfiguration());
  58.  
  59.            
  60.         }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement