Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make an OwnsOne property in EF Core 3.0 required when mapping to SQL Server columns? #18445

Closed
KenBrannigan opened this issue Oct 18, 2019 · 4 comments

Comments

@KenBrannigan
Copy link

I have a main entity Profile that has a property Name that is a value object. The Name object has two properties First and Last. When I use the Fluent API to map the Name objects properties to columns within the Profile table I specify that they are required. When I create the migration it says nullable is true. I assume it has to do with the fact that in EF Core 3.0 owned entities are now optional but how do I tell EF that they are actually required?

public class Profile
{
   public Name Name { get; private set; }
   ...
}
public class Name
{
   public string First { get; }
   public string Last { get; }
   ...
}
public override void Configure(EntityTypeBuilder<Profile> builder)
{
   base.Configure(builder);

   builder.OwnsOne(
                navigationExpression: p => p.Name,
                buildAction: n =>
                {
                    n.Property(n => n.First)
                        .HasColumnName("NameFirst")
                        .HasMaxLength(25)
                        .IsRequired();

                    n.Property(n => n.Last)
                        .HasColumnName("NameLast")
                        .HasMaxLength(25)
                        .IsRequired();
                });
}

EF Core version: 3.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 1903 latest updates
IDE: Visual Studio 2019 16.3.5

I also posted this on stackoverflow:

https://stackoverflow.com/questions/58417334/how-to-make-an-ownsone-property-in-ef-core-3-0-required-when-mapping-to-sql-serv

Any help you can provide would be great.

Take care,
Ken

@davidhenley
Copy link

davidhenley commented Oct 18, 2019

As of right now it's not possible without manually changing the migration.

@AndriySvyryd
Copy link
Member

Required owned types are tracked by #12100
However value objects would work better if implemented using value converters: #13947

@m4ss1m0g
Copy link
Contributor

However value objects would work better if implemented using value converters: #13947

Can you provide an example with value converters as value object ?

@AndriySvyryd
Copy link
Member

Can you provide an example with value converters as value object ?

@m4ss1m0g Of course, as soon as it's implemented.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants