与导航属性同名的列没有 ID 的外键
我有以下表格。
CREATE TABLE Thing
(
Id INT PRIMARY KEY IDENTITY(1, 1),
CreatedByUser INT NOT NULL
)
CREATE TABLE User
(
Id INT PRIMARY KEY IDENTITY(1, 1)
)
我有以下 EF 实体,(有意)使用不带 Id 的导航属性进行定义。
public class Thing
{
public int Id { get; set; }
public User CreatedByUser { get; set; }
}
public class User
{
public int Id { get; set; }
}
我想知道当表上的基础 Id 字段与我想要包含在实体模型中的导航属性同名时如何定义外键关系。 以下示例无法找到名为 CreatedByUser_Id 的数据库列。
modelBuilder.Entity<Thing>()
.HasOne(x => x.CreatedByUser)
.WithMany()
.HasForeignKey("CreatedByUser_Id")
.IsRequired();
谢谢
I have the following tables.
CREATE TABLE Thing
(
Id INT PRIMARY KEY IDENTITY(1, 1),
CreatedByUser INT NOT NULL
)
CREATE TABLE User
(
Id INT PRIMARY KEY IDENTITY(1, 1)
)
I have the following EF entities, defined (intentionally) with a navigational property without an Id.
public class Thing
{
public int Id { get; set; }
public User CreatedByUser { get; set; }
}
public class User
{
public int Id { get; set; }
}
I would like to know how to define the foreign key relationship when the underlying Id field on the table is the same name as the navigation property I would like include on the entity model. The following example fails to find a db column with the name CreatedByUser_Id.
modelBuilder.Entity<Thing>()
.HasOne(x => x.CreatedByUser)
.WithMany()
.HasForeignKey("CreatedByUser_Id")
.IsRequired();
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用以下内容修复了它。
Fixed it with the following.