Home > Back-end >  Problems with creating data, ASP.NET Core
Problems with creating data, ASP.NET Core

Time:10-21

I have this problem with relationships when I try to make database.

There are my classes and error

The seed entity for entity type 'ApplicationUser' with the key value 'ApplicationUserId:64e44d8c-7b8b-4cde-847f-2037c888156f' cannot be added because it has the navigation 'Paintings' set. To seed relationships, add the entity seed to 'Painting' and specify the foreign key values {'ApplicationUserId'}.

Code:

public class ApplicationUser 
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ApplicationUserId { get; set; } = Guid.NewGuid();

    [Required]
    [StringLength(40, ErrorMessage = "You must write your name", MinimumLength = 3)]
    public string? Name { get; set; }

    [Required]
    [StringLength(40, ErrorMessage = "You must write your SecondName", MinimumLength = 3)]
    public string? SecondName { get; set; }

    [Required]
    [StringLength(60, ErrorMessage = "The password must have less than 60 and more than 8 symbols", MinimumLength = 8)]
    public string? Password { get; set; }

    [Required]
    [RegularExpression(@"^([\w\.\-] )@([\w\-] )((\.(\w){2,3}) )$", ErrorMessage = "Incorrect email! Example: [email protected]")]
    public string? Email { get; set; }

    [Required]
    [RegularExpression(@"\d{3}-\d{3}-\d{4}", ErrorMessage = "Incorrect format of number! Example:XXX-XXX-XXXX")]
    public string? PhoneNumber { get; set; }

    [Required]
    [StringLength(500, ErrorMessage = "You must write your SecondName", MinimumLength = 0)]
    public string? About { get; set; }
    public string? ImgURL { get; set; }

    public List<Painting>? Paintings { get; set; }
}

public class Painting
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid PaintingId { get; set; } = Guid.NewGuid();
    [Required]
    [StringLength(30, MinimumLength = 3)]
    public string? Name { get; set; }
    [Required]
    [StringLength(5, MinimumLength = 0)]
    public string? Subject { get; set; }
    [Required]
    [StringLength(30, MinimumLength = 0)]
    public string? Style { get; set; }
    [Required]
    [StringLength(30, MinimumLength = 0)]
    public string? Medium { get; set; }
    [Required]
    public decimal? Price { get; set; }
    [Required]
    public string? Size { get; set; }
    [Required]

    public string? ImgURL { get; set; }

    [StringLength(30, MinimumLength = 0)]
    public string? Autor { get; set; }

    [StringLength(200, MinimumLength = 0)]
    public string? About { get; set; }

    public Guid ApplicationUserId { get; set; }
    [ForeignKey("ApplicationUserId")]
    public ApplicationUser? ApplicationUser { get; set; }
}

CodePudding user response:

for automatic GUID creation

 builder.Property(p => p.Rowguid).HasDefaultValueSql("(newid())")  

for the email field add the annotation

[DataType(DataType.EmailAddress)]

CodePudding user response:

From the error message it looks like seed data you are using for ApplicationUser also has a collection of Paintings like so

new ApplicationUser{
  Paintings = new List<Painting>{
    new Painting(),
    new Painting()
  } 
}

This won't work with EF core, navigation properties must not be populated in your seed data. What this means is that you must add code to seed your Painting model separately from ApplicationUser, but since they are related you must specify the foreign key ApplicationUserId with the Guid values that you set for ApplicationUser: e.g.

    modelBuilder.Entity<ApplicationUser>().HasData(new List<ApplicationUser>
    {
        new ApplicationUser
        {
            ApplicationUserId = new Guid("64e44d8c-7b8b-4cde-847f-2037c888156f")
        }
    });

    modelBuilder.Entity<Painting>().HasData(new List<Painting>
    {
        new Painting
        {
            ApplicationUserId = new Guid("64e44d8c-7b8b-4cde-847f-2037c888156f")
        }
    });  

So we have two different HasData method declarations the first to seed ApplicationUser and the second to seed Painting and you will also notice that am using "64e44d8c-7b8b-4cde-847f-2037c888156f" as the primary key value for ApplicationUser and as the foreign key value for Painting, with this Painting will be linked to ApplicationUser on the same reference id.

And also looking at your models I can see you are generating Guid values for them, you don't have to do that.

public Guid PaintingId { get; set; } //= Guid.NewGuid();
  • Related