Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Issues with complex objects #2020

Description

@X0RN0T

Describe the bug
When working with Pomelo.EntityFrameworkCore.MySql Version 9 in my Blazor application i tried to work with the objects

[Table("Users")]
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public Guid Guid { get; set; }

[MaxLength(256)]
[Column("firstname", TypeName = "varchar(256)")]
public string Firstname { get; set; } = string.Empty;

[MaxLength(256)]
[Column("lastname", TypeName = "varchar(256)")]
public string Lastname { get; set; } = string.Empty;

}

[Table("Restaurants")]
public class Restaurant
{
public int Id { get; set; }

[Required]
[MaxLength(200)]
public string Name { get; set; } = string.Empty;

[MaxLength(1024)]
public string? GoogleShareLink { get; set; }

public List<Ranking> Rankings { get; set; } = new();

public List<Scoring> Scorings { get; set; } = new();

[Column(TypeName = "date")]
public DateTime? ProposedDate { get; set; }

[Column(TypeName = "date")]
public DateTime? VisitedDate { get; set; }

public int CreatorId { get; set; }

public User Creator { get; set; }

}

[Table("Scorings")]
public class Scoring
{
[Key]
public int Id { get; set; }

public int Score { get; set; }

[ForeignKey(nameof(Creator))]
public int CreatorId { get; set; }

public User Creator { get; set; }

[ForeignKey(nameof(Restaurant))]
public int RestaurantId { get; set; }

public Restaurant Restaurant { get; set; }

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

}

[Table("Rankings")]
public class Ranking
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Range(0, 5)]
public double Score { get; set; }

[MaxLength(1024)]
public string ChoosenMeal { get; set; }

[MaxLength(2024)]
public string? Comment { get; set; }

[ForeignKey(nameof(Restaurant))]
public int RestaurantId { get; set; }

[JsonIgnore]
public Restaurant Restaurant { get; set; }

[ForeignKey(nameof(RankingCreator))]
public int RankingCreatorId { get; set; }

[JsonIgnore]
public User RankingCreator { get; set; }

[Column(TypeName = "date")]
public DateTime RankingDate { get; set; }

}

I tried to used my controller like

// POST: api/restaurants
[HttpPost]
public async Task<ActionResult> CreateRestaurant(Restaurant restaurant)
{
_context.Restaurants.Add(restaurant);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetRestaurantById), new { id = restaurant.Id }, restaurant);
}

gets Error "MySqlException: Duplicate entry '1' for key 'PRIMARY'"

Database is a Maria DB Version(10, 6, 22)

Also gets often error "StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.BrowserHttpContent, Headers:
{
Content-Type: application/problem+json; charset=utf-8
}"

To Reproduce
Build a PWA Blazor application and a corresponding .NET API. Design the models and the api controller, call the api

Expected behavior
Should work without error

Technical details (please complete the following information):

  • Database server version: Maria DB Version(10, 6, 22)
  • Operating system: Windows 11
  • Pomelo.EntityFrameworkCore.MySql version: 9.0.0
  • Other technical details: ASP.NET Core

Additional context
I tried to find the error in designing the models best as possible but nothing changed the problems. A workaround that works was to pass a restaurant object to the api (for not getting the bad request error) and delete it in the controller (for not getting the Duplicate entry error). But i´m sure that shouldn`t be the way

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions