Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 2.0 OwnsOne column prefix

Tags:

ef-core-2.0

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?

like image 797
Tommy Jakobsen Avatar asked Jan 10 '18 12:01

Tommy Jakobsen


2 Answers

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

   public static void 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(""));
like image 144
Jeremy Lakeman Avatar answered Jan 02 '23 22:01

Jeremy Lakeman


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

like image 23
AnorZaken Avatar answered Jan 02 '23 22:01

AnorZaken