How to specify default property values for owned entity types in Entity Framework Core 2.0?

asp.net entity-framework-core

Question

I have a simple POCO type, say something like

public class OwnedEntity {

    public string stringProperty { get; set; }
    public decimal decimalProperty { get; set; }
    public bool boolProperty { get; set; }
    public int intProperty { get; set; }
}

and an actual entity with an OwnedEntity reference

public class SomeEntity {

    public string Id { get; set; }
    public OwnedEntity OwnedEntity { get; set; }
}

I set up the relationship like described in the documentation using EF Core's Fluent API:

protected override void OnModelCreating (ModelBuilder builder) {

    base.OnModelCreating (builder);

    builder.Entity<SomeEntity> ().OwnsOne (e => e.OwnedEntity);
}

I can't find anything on how to define default-values for all the properties of OwnedEntity. I tried to initialize the properties like this:

public class OwnedEntity {

    public string stringProperty { get; set; } = "initial"
    public decimal decimalProperty { get; set; } = -1M;
    public bool boolProperty { get; set; } = false;
    public int intProperty { get; set; } = -1;
}

but with no effect. Same goes with the [DefaultValueAttribute] (but that was to expect since it's explicitly mentioned).

There's a bit of information on how to handle initial values for regular entities:

modelBuilder.Entity<SomeOtherEntity>()
        .Property(e => e.SomeIntProperty)
        .HasDefaultValue(3);

But since I'm facing an Owned Entity Type, I can't access the type via Entity<T>.

Is there a way of doing what I'm looking for?

Some things worth mentioning:

  • I have a solid amount of specific entities where most of them are using the OwnsOne relation
  • Declaring all OwnedEntity-properties in a base class is not an option since not all the entities have those properties
  • I`m using EF Core 2.0.3 and ASP.NET Core MVC 2.0.4

Edit:

Originally, I wanted to have newly created SomeEntity instances to come with preset properties for all of the 'embedded' SomeEntity.OwnedEntity properties.

But looking at how my associated controller works, it all makes sense... I have the following methods for the 'Create' operation:

[HttpGet]
public IActionResult Create () {
    return View (nameof (Create));
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create (SomeEntity model) {
    context.Add (model);
    await context.SaveChangesAsync ();

    // redirect etc.
}

Which means that no object is created for the [HttGet] overload of Create and all the HTML inputs linked to properties (via asp-for) are initially empty. Okay. So I guess the proper way of doing this is to manually create a new instance of SomeEntity and pass it to the Create view like this:

[HttpGet]
public IActionResult Create () {
    return View (nameof (Create), new SomeEntity());
}

Is this the right approach then or are there some more things to keep in mind?

1
3
6/11/2018 8:17:54 PM

Popular Answer

Assuming you understand what EF Core Default Values are for, and just looking for equivalent of Entity<T>().Property(...) equivalent.

The owned entities are always configured for each owner type by using the ReferenceOwnershipBuilder<TEntity,TRelatedEntity> class methods. To access this class you either use the result of OwnsOne method, or use the OwnsOne overload taking second argument of type Action<ReferenceOwnershipBuilder<TEntity,TRelatedEntity>>.

For instance, using the second approach:

builder.Entity<SomeEntity>().OwnsOne(e => e.OwnedEntity, ob =>
{
    ob.Property(e => e.stringProperty)
        .HasDefaultValue("initial");
    ob.Property(e => e.decimalProperty)
        .HasDefaultValue(-1M);
    // etc.
});
3
6/11/2018 6:44:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow