question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] - [Required] attribute ignored when creating column named Id

See original GitHub issue

When creating a new database using EnsureCreated(), a column with the name Id is created as nullable even though the required attribute is placed on the property. I believe this is due to it being in a derived class with the Primary Key annotated in the base class. Perhaps the order of property evaluation affects the outcome.

  public class MyBaseClass
    {
        [Key]
        public int MyKey { get; set; }
    }

    [Table("Test")]
    public class MyDerivedClass : MyBaseClass, IEntityTypeConfiguration<MyDerivedClass>
    {
        [Required]
        public string Id { get; set; }

        
        void IEntityTypeConfiguration<MyDerivedClass>.Configure(EntityTypeBuilder<MyDerivedClass> builder)
        {
            _ = builder.HasKey(x => x.MyKey);
            _ = builder.HasIndex(x => x.Id).IsUnique();
        }
    }

image

With the above definition, a table is created with an auto-generated primary key “MyKey” and a nullable column “Id”

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, May 4, 2021

@omnilogix as a workaround, you can either change the order of the properties as you’ve done, or change the name of the Id property, this way EF doesn’t initially configure it as the key by convention.

1reaction
ajcvickerscommented, May 3, 2021

HasBaseType should not be needed in this case. Just don’t include the base type in the model. Typical ways the base type is included in the model:

  • Having a DbSet property for the type
  • Having a call to ModelBuilder.Entity for the type
  • Having an IEntityTypeConfiguration for the type
  • Referencing it from another type in the model

Since there isn’t full repro code here I can’t tell which of these is being used. If you are not doing any of these things and the type is still included, then please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot + JPA : Column name annotation ignored
SQL generated by this created test_name as the columns name. After looking for a solution I have found that spring.jpa.hibernate.naming_strategy ...
Read more >
Required attribute is ignored on Dropdown and Radio field
It seems that Site Columns defined as Type="Choice" ignore the Required="TRUE" attribute when it comes to form validation on the Edit/New pages.
Read more >
How to fix invalid 'Invalid column name' after adding a ...
Hi,. I have added a column in to my existing table. But, it is giving me an error. I don't know what is...
Read more >
Code First Data Annotations - EF6
One is using simple attributes called DataAnnotations, ... Adding Required to the Title property will force EF (and MVC) to ensure that the ......
Read more >
Error Explanations for The W3C Markup Validation Service
This error message means that the name of an attribute and the equal sign cannot be omitted when specifying an attribute. A common...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found