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

Better error message when using nav prop in string overload of Property #5056

Closed
ansydor opened this issue Apr 13, 2016 · 2 comments
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ansydor
Copy link

ansydor commented Apr 13, 2016

I get an exception on migration adding:

System.InvalidOperationException: The property 'CreatedBy' cannot be added to the entity type 'Post' because a navigation property with the same name already exists on entity type 'Post'.

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {

        public virtual DbSet<User> DomainUsers { get; set; }
        public virtual DbSet<Post> Posts { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity<User>(entity =>
            {
                entity.HasKey(key => key.Id);
                entity.Property(property => property.Email).IsRequired(true);
                entity.Property(property => property.Login).IsRequired(true);
                entity.Property(property => property.Created).IsRequired(true);
                entity.Property(property => property.Updated).IsRequired(true);
            });

            builder.Entity<Post>(entity =>
            {
                entity.HasKey(key => key.Id);
                entity.Property(property => property.Title).IsRequired(true);
                entity.Property(property => property.Content).IsRequired(true);
                entity.Property(property => property.Created).IsRequired(true);
                entity.Property<User>(property => property.CreatedBy).IsRequired(true);
                entity.Property(property => property.Updated).IsRequired(true);
                entity.Property<User>(property => property.UpdatedBy).IsRequired(true);
            });
        }
    }

post entity

public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public DateTimeOffset Created { get; set; }
        public User CreatedBy { get; set; }
        public DateTimeOffset Updated { get; set; }       
        public User UpdatedBy { get; set; }
    }
@AndriySvyryd
Copy link
Member

The Property method cannot be used to configure navigation properties. You would need to use the relationship API to accomplish it:

entity.HasOne(e => e.CreatedBy).WithMany().IsRequired()

We don't provide a Navigation() method to avoid creating under-configured relationships.

@rowanmiller Could you come up with an exception message that conveys this?

@rowanmiller rowanmiller modified the milestones: 1.0.1, 1.0.0 May 11, 2016
@zabulus
Copy link

zabulus commented May 14, 2016

Just encountered the same. Exception message should be changed to not confuse users.

@rowanmiller rowanmiller changed the title Problem with simple reference Better error message when using nav prop in string overload of Property Jul 1, 2016
@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
@rowanmiller rowanmiller added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 7, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

6 participants