EF Core 2.0 OwnsOne column prefix

前端 未结 2 889
暗喜
暗喜 2021-02-10 04:10

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 ma

相关标签:
2条回答
  • 2021-02-10 04:48

    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.)

    0 讨论(0)
  • 2021-02-10 05:08

    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(""));
    
    0 讨论(0)
提交回复
热议问题