我有以下类(class)
public class TreeItem
{
public long Id { get; set; }
public Tree Tree { get; set; }
public string Model { get; set; }
public string Version { get; protected set; }
public string Index { get; protected set; }
}
我想更改默认数据库的属性树的列名。作为在数据库中查找“TreeId”列的默认 Entity Framework ,但我们的名称约定表明此列应命名为“tree_id”
如果我尝试将其设置如下:
modelBuilder.Entity<TreeItem>().Property(t => t.Tree).HasColumnName("tree_id");
Ef 抛出异常消息:
Cannot call Property for the property 'Tree' on entity type 'TreeItem' because it is configured as a navigation property. Property can only be used to configure scalar properties.
是否可以将属性树的默认列名称从“TreeId”更改为“tree_id”?
最佳答案
尝试使用 MapKey():
modelBuilder.Entity<TreeItem>()
.HasRequired(it => it.Tree)
.WithMany()
.Map(m => m.MapKey("tree_id"));
编辑
对于 EF Core,您可能必须使用提到的变通方法 here :
modelBuilder.Entity<TreeItem>()
.Property<long>("TreeId")
.HasColumnName("tree_id");
关于c# - 更改导航属性的默认列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46091157/