- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经在 this 之后使用 .Net core
和 EFCore
创建了一个 API使用 VSCode 的教程。
我的 MySQL 数据库有很多模型,因为我正在“迁移”我的 EF6
和 asp.net WebAPI
到 .Net core
,所以我只是复制并粘贴以避免大量工作(再次)。
当我尝试执行简单的 Get
时,EFCore 正在连接不同表的两列:
Controller 用户
[Route("v1/[controller]")]
public class UserController : Controller
{
private readonly IntentContext _context;
public UserController(IntentContext context)
{
_context = context;
}
[HttpGet]
public IEnumerable<user> GetAll()
{
return _context.user.ToList();
}
}
“用户”模型
public class user
{
public user()
{
user_image = new HashSet<user_image>();
user_credit_card = new HashSet<user_credit_card>();
user_pocket_history = new HashSet<user_pocket_history>();
user_pocket = new HashSet<user_pocket>();
//A lot of table instances
}
public string id { get; set; }
public string username { get; set; }
public string password { get; set; }
public string id_account_type { get; set; }
public int user_status { get; set; }
public DateTime create_time { get; set; }
public DateTime update_time { get; set; }
public virtual account_type account_type { get; set; }
public virtual ICollection<user_image> user_image { get; set; }
public virtual ICollection<user_credit_card> user_credit_card { get; set; }
public virtual ICollection<user_pocket_history> user_pocket_history { get; set; }
public virtual ICollection<user_pocket> user_pocket { get; set; }
//A lot of table relations
}
表account_type
public class account_type
{
public account_type()
{
this.user = new HashSet<user>();
this.establishment_employee = new HashSet<establishment_employee>();
}
public string id { get; set; }
public string account_description { get; set; }
public string account_name { get; set; }
public DateTime create_time { get; set; }
public DateTime update_time { get; set; }
public virtual ICollection<user> user { get; set; }
public virtual ICollection<establishment_employee> establishment_employee { get; set; }
}
get请求时的终端日志
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (140ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT `u`.`id`, `u`.`Id_account_type`, `u`.`account_typeid`, `u`.`create_time`, `u`.`password`, `u`.`update_time`, `u`.`user_status`, `u`.`username`
FROM `user` AS `u`
MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'u.account_typeid' in 'field list' ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column **'u.account_typeid'** in 'field list'
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
//A lot of exceptions generated
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred in the database while iterating the results of a query for context type 'IntentAPI.Models.IntentContext'.
MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'u.account_typeid' in 'field list' ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'u.account_typeid' in 'field list'
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
请注意,字段 u.account_typeid
确实不存在。它是 user
表的 account_type
和 account_type
表的 id
的串联。
为什么会这样?
非常感谢和抱歉我糟糕的英语
最佳答案
这在 Relationships - No Foreign Key Property 中有解释EF Core 文档部分:
If no foreign key property is found, a shadow foreign key property will be introduced with the name
<navigation property name><principal key property name>
您需要通过任一数据注释为 user.account_type
导航属性指定 FK 属性:
[ForeignKey("id_account_type")]
public virtual account_type account_type { get; set; }
或流畅的 API:
modelBuilder.Entity<user>()
.HasOne(e => e.account_type)
.WithMany(e => e.user)
.HasForeignKey(e => e.id_account_type);
关于c# - EFCore 选择了错误的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054611/
自动生成字段值,咱们首先想到的是主键列(带 IDENTITY 的主键)。EF Core 默认的主键配置也是启用 Identity 自增长的,而且可以自动标识主键。前提是代表主键的实体属性名要符合以下规
上次老周扯了有关主、从实体的话题,本篇咱们再挖一下,主、从实体之间建立的关系,跟咱们常用的一对1、一对多这些关系之间有什么不同。 先看看咱们从学习数据库开始就特熟悉的常用关系——多对多、一对
前言 基于EF Core + MySQL的基本增删改查,示例是基于 .NET6 + EF Core + MySQL 创建实体和数据库、EFCore 数据迁移 项目基础上的内容增加。同时也是
前言 EFCore.BulkExtensions是一个常用的EF core 批量处理数据的库. 但是支持的数据库相对较少.特别是.NET5.0版本 连MySQL都无法支持 这个库就是改造的
我试图在我的上下文类中设置一对一的外键关系,但我不断收到以下错误。我找到了一个 similar post但这似乎并没有解决我的问题,因为我在模型中明确指定了关系。我确认: Server 对象引用了一个
运行 dotnet ef migrations add XYZ将导致 a Migrations directory being created在项目中。这个目录是否应该提交给版本控制(Git 等)?
我试图围绕 EF Cores 拥有的对象以及如何控制何时加载某些数据块。 基本上我有一堆旧的遗留表(一些有大约 150 列),并希望使用一个根实体和每个表的几个拥有的对象对它们进行建模,以实现更好的分
我的模型包含 Post 和 PostHistory 类,其中 Post 与 PostHistory 具有一对多关系。 class Post { public int Id { get; set
我有一个 .Net Core WebApplication 项目,其中上下文类位于类库中。如果我在 OnConfiguring(DbContextOptionsBuilder optionsBuild
我正在尝试使用 Entity Framework Core 2.0.1 将现有的 PostgreSQL 数据库转换为实体,但在搭建脚手架时出现错误。数据库是通过运行以下脚本创建的: Script To
我在一个项目中有两个实体:SupplierFinishingItem 和 ProductOptionListItem。 ProductOptionListItem 通过导航属性引用另一个。 当我尝试创
我已经在 this 之后使用 .Net core 和 EFCore 创建了一个 API使用 VSCode 的教程。 我的 MySQL 数据库有很多模型,因为我正在“迁移”我的 EF6 和 asp.ne
目录 一 代码分析 1 GetTableName 2&n
我正在尝试根据实体特定字段中的搜索值列表来过滤实体。 例如: var searchValues = new List { "abc", "xyz" }; var posts = Context.Pos
我有一个在表中创建列的迁移。 public partial class AddName : Migration { protected override void Up(MigrationBu
Ef Core 接收错误 System.InvalidOperationException: Can't process set operations afterclient evaluation,
我有以下型号。在使用 find 方法从数据库中获取时,用子实体加载父实体的更好方法是什么? 父实体: public class Client { public int Id { get; se
我正在使用 EFCore 做一个原型(prototype),并努力寻找一种返回我刚刚添加到实体集合中的对象的好方法。 例如: public Songleader AddSongleader(int c
这个问题已经有答案了: How is Identity.EntityFramework OnModelCreating called (2 个回答) 已关闭12 个月前。 我知道当您创建迁移时它会被调
这个问题已经有答案了: How is Identity.EntityFramework OnModelCreating called (2 个回答) 已关闭12 个月前。 我知道当您创建迁移时它会被调
我是一名优秀的程序员,十分优秀!