ef mysql 外键 一对一_EFCore-一对一配置外键小记2
前后兩次遇到這樣的錯誤:
The property 'xx' on entity type 'xxxx' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
多數情況下是表配置關系會出現這樣的問題。我實在配置TagItem一對一關聯TagUseCount出現的問題:
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("Tags")
.HasKey(x => x.Id);
builder.Property(x => x.Id).HasColumnName("TagId");
builder.Property(x => x.TagName);
builder.HasOne(x => x.TagUseCount)
.WithOne(x => x.Tag)
.HasForeignKey(x => x.Id);
}
以及
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("TagUseCount");
builder.Property(t => t.Id)
.HasColumnName("TagId");
builder.Property(t => t.UseCount);
}
以上配置有兩點錯誤,都是調試后總結出來的:
TagItem 關聯TagUseCount 需要指定的是關聯表的外鍵,也就是TagUseCount的Id,不能指定自己哦!
TagUseCount千萬別偷懶或忘記寫 builder.HasKey(x => x.Id);
總結
以上是生活随笔為你收集整理的ef mysql 外键 一对一_EFCore-一对一配置外键小记2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器人蛮王_盖伦:吊打我老婆,蛮王:我也
- 下一篇: 归母净利润是什么意思