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

Support of unique Index with Owned types properties #12637

Closed
thiagomajesk opened this issue Jul 11, 2018 · 5 comments
Closed

Support of unique Index with Owned types properties #12637

thiagomajesk opened this issue Jul 11, 2018 · 5 comments

Comments

@thiagomajesk
Copy link

Hi, I wonder if this is supported...

I have an owned type already configured and I wish to add a unique constraint that includes one of the owned type properties to prevent the user to insert two entities with "same" info.

For clarification:

class MyType
{
    private string description;
    public string Description => description;
}
class Bar
{
    public virtual ICollection<Foo> Foos { get; set; }
}
class Foo
{
    public virtual Bar Bar { get; set; }
    public MyType Buzz { get; set; }
}
 modelBuilder.Entity<Foo>(x =>
{
    x.OwnsOne(z => z.Buzz)
     .Property(z => z.Description).HasColumnName("Buzz")
     .UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction)
     .IsRequired();

      // Only want to allow one pair of BarId + Buzz to exist in the db
      x.HasIndex("BarId", "Buzz").IsUnique(); // does not work
});

The error I'm getting is...

The property or navigation 'Buzz' cannot be added to the entity type 'Foo' because a property or navigation with the same name already exists on entity type 'Foo'.

Is there any currently supported way to get this working?

@divega
Copy link
Contributor

divega commented Jul 16, 2018

@AndriySvyryd, thoughts?

@AndriySvyryd
Copy link
Member

@thiagomajesk You can just create the index on the owned type:

modelBuilder.Entity<Foo>(x =>
{
    x.OwnsOne(z => z.Buzz, zb =>
    {    
         zb.Property(z => z.Description).HasColumnName("Buzz")
           .UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction)
           .IsRequired();
         
         zb.HasIndex(z => z.Description).IsUnique();
    });
});

@thiagomajesk
Copy link
Author

thiagomajesk commented Jul 16, 2018

@AndriySvyryd This won't work because I don't want "Description" to be unique by itself, I want uniqueness on the pair: BarId + Buzz (Description). Configuring it separately does not solve the problem for me.

@AndriySvyryd
Copy link
Member

Then this is a duplicate of #11336, see the comments there for a workaround.

@thiagomajesk
Copy link
Author

@AndriySvyryd Ok, thanks!

@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

4 participants