EF Core 2.0 OwnsOne column prefix

这一生的挚爱 提交于 2021-02-18 00:50:50

问题


When using OwnsOne to map complex types, the sql column name is prefixed with the attribute name. Is it possible to specify the prefix name in the mapping?

This is my mapping:

e.OwnsOne(x => x.Attributes, cb =>
{
    cb.OwnsOne(a => a.Supplier);
});

I would like the sql column to be prefixed with "Attr_" Instead of "Attributes_". Is this possible?


回答1:


Ivan Stoev's answer from the question comments:

It has to be done through the corresponding OwnsOne builder action argument. e.g. .OwnsOne(e => e.Address, cb => { cb.Property(e => e.Postcode).HasColumnName("Postcode"); });

(Making this a community wiki, just marking the question as being answered.)




回答2:


You could write an extension method to override the names of all columns;

   public static WithPrefix<T, R>(this OwnedNavigationBuilder<T, R> builder, string prefix) where T:class where R:class
   {
      foreach (var p in builder.OwnedEntityType.GetProperties())
         p.SetColumnName($"{prefix}{p.Name}");
   }

   .OwnsOne(e => e.Address, cb => cb.WithPrefix(""));


来源:https://stackoverflow.com/questions/48187344/ef-core-2-0-ownsone-column-prefix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!