【问题标题】:Asp net core api post request with many to many controller带有多对多控制器的 Asp net core api 发布请求
【发布时间】:2021-11-18 00:47:13
【问题描述】:

我有一个简单的问题。我有 2 种模型、鸡尾酒和配料:

    public class Coctails
{
    [Required]
    public long CoctailsId { get; set; }

    public string Name { get; set; }

    public string CookingMethod { get; set; }

    public double Price { get; set; }

    public List<Ingredients> CoctailIngredients { get; set; }
}

    public class Ingredients
{

    [Required]
    public long IngredientsId { get; set; }

    
    public string Name { get; set; }

    public double Amount { get; set; }

    public double Price { get; set; }

    public List<Coctails> CoctailsWithThisIngredient { get; set; }
}

我正在使用邮递员保存 1 种成分,并提出以下要求:

{
"Name":"vodka",
"Amount":1000,
"Price":100}

之后我尝试通过发布请求添加新的鸡尾酒:

{
"Name":"vodka s pivom" ,
"CookingMethod":"rubilovo",
"Price":100,
"coctailIngredients":
[
    {
        "Name" : "vodka",
        "Amount" : 50
    }
]}

而且我有无限循环错误(但我需要这 2 个字段 CoctailIngredients 和 CoctailsWithThisIngredient 告诉 ef 这是多对多关系,它正在为我制作第三个连接表)......那么如何避免这种情况?

我的数据库上下文类:

    public class DatabaseContext : DbContext
{
    public DatabaseContext()
    {
    }

    public DbSet<Coctails> Coctails { get; set; }
    public DbSet<Ingredients> Ingredients { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Coctails>()
                .HasMany(c => c.CoctailIngredients)
                .WithMany(s => s.CoctailsWithThisIngredient)
                .UsingEntity(j => j.ToTable("CoctailsWithIngredients"));
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=BarDb.db");
    }


}

我的 POST 方法:

    [HttpPost]
    public async Task<ActionResult<Coctails>> PostCoctails(Coctails coctails)
    {
        _context.Coctails.Attach(coctails);

        await _context.SaveChangesAsync();

        return CreatedAtAction("GetCoctails", new { id = coctails.CoctailsId }, coctails);
    }

【问题讨论】:

  • 你的发帖方式是什么样的?
  • 添加到问题中

标签: c# entity-framework .net-core asp.net-web-api entity-framework-core


【解决方案1】:

保持多对多关系的唯一方法是添加第三个表。如果您使用 Ef core 5+,则 ef 可以在影子中为您创建此表,但最好显式创建它:

public class CoctailIngridient
{
     [Key]
    public long Id { get; set; }

    public long CoctailId { get; set; }
    public long IngredientId { get; set; }
    public virtual Coctail Coctail { get; set; }
    public virtual Ingredient Ingridient { get; set; }
 }

public class Coctail
{
    [Key]
    public long CoctailsId { get; set; }

    public string Name { get; set; }

    public string CookingMethod { get; set; }

    public double Price { get; set; }

    public List<CoctailIngredient> CoctailIngredients { get; set; }

    //only for ef core 5+
    public List<Ingredient> Ingredients { get; set; }
 }

 public class Ingredient
{

    [Key]
    public long IngredientId { get; set; }

    
    public string Name { get; set; }

    public double Amount { get; set; }

    public double Price { get; set; }

     public List<CoctailIngredient> CoctailIngredients { get; set; }

     //only for ef core 5+
    public List<Coctail> Coctails { get; set; }
}

【讨论】:

  • 手动添加第三个表没有帮助。错误仍然重现,成分表中出现新记录:(
  • 我应该在数据库上下文中添加第三个表吗?你能展示 OnModelCreating 方法吗?
【解决方案2】:

添加第三个模型类并从数据库上下文方法中删除 OnModelCreating 方法可以解决问题。我想问题已经解决了。感谢您的帮助!

还有一件非常重要的事情,我还在我的上方添加了“NotMapped”标签

public List<Coctails> CoctailsWithThisIngredient { get; set; }

public List<Ingredients> CoctailIngredients { get; set; }

【讨论】:

  • 您需要有明确的第三张表来存储鸡尾酒所需的配料量。至少如果你不想在数据库中有很多同名的成分。
  • 这没有多大意义。如果您不映射属性,则不会填充它们。您是否阅读了有关多对多关联的 EF 文档?
  • 当然我做到了,但我完全不知道如何......但它有效!除了上面所描述的,我什么都没做
猜你喜欢
  • 2020-06-19
  • 2021-03-12
  • 2014-09-06
  • 1970-01-01
  • 2019-01-06
  • 2022-10-19
  • 2021-06-25
  • 1970-01-01
  • 2021-03-30
相关资源
最近更新 更多