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

IsRequired(false) created same field name with additional 1 #517

Closed
gohckok11 opened this issue Jul 13, 2018 · 8 comments
Closed

IsRequired(false) created same field name with additional 1 #517

gohckok11 opened this issue Jul 13, 2018 · 8 comments

Comments

@gohckok11
Copy link

gohckok11 commented Jul 13, 2018

public class Table1
{
        public int Table1Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Table2> MyTables { get; set; }
}

public class Table2
{
        public int Table2Id { get; set; }
        public int Table1Id { get; set; }
        [ForeignKey("Table1Id")]
        public virtual Table1 Table1 { get; set; }
}

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Table1>()
        .HasMany(e => e.MyTables)
        .WithOne(e => e.Table1)
        .IsRequired(false);
}

PostgresException: 42703: column t.Table1Id1 does not exist

The above code generated additional field that is invalid. Removing false in IsRequired() will operate normally. I am currently using Npgsql.EntityFrameworkCore.PostgreSQL v2.1.1.

@roji
Copy link
Member

roji commented Jul 13, 2018

It seems reasonable for Table1Id1 to get created: you're defining an optional relationship from Table2 to Table1, explicitly specifying the foreign key to be Table1Id, but defining Table1Id to be non-nullable... Have you tried making Table1Id an int? intead of an int`?

Regardless, this issue doesn't seem like it's specific to Npgsql in any way (it would probably happen in Sqlite, SQL Server...) so it belongs in the EF Core repo, if you're still encountering issues please move it there.

@roji roji closed this as completed Jul 13, 2018
@ajcvickers
Copy link

/cc @divega @AndriySvyryd This is not unreasonable behavior, but I expected an exception instead that said the FK wasn't nullable, so just pinging you guys to take a look.

@AndriySvyryd
Copy link

@ajcvickers This is by design. The explicit 'IsRequired' overrides the FK property set through the attribute.
We could consider logging this, but I don't think it would help, as we already log the creation of the shadow property and that wasn't seen by the user.

@divega
Copy link

divega commented Jul 13, 2018

@AndriySvyryd I think a warning might help other customers if not everyone.

@AndriySvyryd
Copy link

@divega it will also produce more log spam for customers that are ok with the behavior.

@divega
Copy link

divega commented Jul 13, 2018

I get that there is a compromise, but to me this one seems to be on the border of being invalid configuration. Do you think the chances that anyone would do this on purpose are high? I don't, but maybe I am missing something.

@ajcvickers
Copy link

It seems unlikely to me that this would be intentional, so a warning makes sense to me.

@AndriySvyryd
Copy link

Filed dotnet/efcore#12693

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants