gpt4 book ai didi

c# - 无效的列名 "User_Id"

转载 作者:行者123 更新时间:2023-12-02 04:54:49 28 4
gpt4 key购买 nike

我收到有关“User_Id”列的异常,我的数据库或代码中没有该列。它就这样出现了......有什么问题?有什么建议 ?当我想添加一些东西时,我得到了这个异常。

        protected override void BaseAdd(Meal entity)
{
using (var context = new ProjectContext())
{
context.Meals.Add(entity);
context.SaveChanges(); //here is where I get the exception
}
}

最佳答案

你有 User_Id 因为你映射了 User 可以像这样在你的 POCO 中有多个 Meals

public class User
{
public int Id { get; set; }
public int Name { get; set; }

public ICollection<Meal> Meals { get; set; }
}

因为 EF 自动假定 MealUser 具有一对多关系。

我建议您首先执行 update-database -v -f 以确保您的数据库设置正确。其次 - 我强烈建议在你的 POCO 中也制作关系 ID,这样你的数据库与你的 POCO 是 1-1 的,你不会得到像这样的任何惊喜。

关于c# - 无效的列名 "User_Id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18184202/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com