[Bug] - [Required] attribute ignored when creating column named Id
See original GitHub issueWhen 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();
}
}
With the above definition, a table is created with an auto-generated primary key “MyKey” and a nullable column “Id”
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
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:DbSet
property for the typeModelBuilder.Entity
for the typeIEntityTypeConfiguration
for the typeSince 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.