-1

I am using EF Core Code-First Approach to configure/map an entity class named Student to SQL Server table named Student. The Student entity model class has a string column named Name that will be mapped to a SQL Server column named "Name" that belongs to nvarchar and has maximum length of 50.

Currently, to get what I configure for the Name column, I call 2 extension methods that are HasMaxLength() and HasColumnType():

builder.Property(s => s.Name).IsRequired(true).HasMaxLength(50).HasColumnType("nvarchar");

My question: I'd like to combine those 2 extension methods to 1 extension method HasColumnType() as follows. But I am not sure that works or not. Please help.

 builder.Property(s => s.Name).IsRequired(true).HasColumnType("nvarchar(50)");

Details of my Student entity/column configuration class:

public class StudentConfiguration: IEntityTypeConfiguration<Student>
    {
        public void Configure(EntityTypeBuilder<Student> builder)
        {
            
            // 1) Table
            builder.ToTable("Student");

            // 2) Key
            builder.HasKey(s => s.StudentId);
            builder.Property(s => s.StudentId).UseIdentityColumn(1, 1);

            // 2) Properties
            builder.Property(s => s.Name).IsRequired(true).HasMaxLength(50).HasColumnType("nvarchar");
            ...


            ...
        }
    }

And my Student entity class:

public class Student
    {
        public long StudentId { get; set; }
        public string Name { get; set; }
        public int? Age { get; set; }
        public bool IsRegularStudent { get; set; }
       
    }

[Updated] The answer is Yes. I can combine 2 method calls HasMaxLength(50).HasColumnType("nvarchar")

into 1 method call

HasColumnType("nvarchar(50)")

Finally, the result is EF Core generates SQL server table Name column as:

Name nvarchar(50) NULL
Thomas.Benz
  • 8,381
  • 9
  • 38
  • 65
  • 1
    Is there a reason you want to achieve this? You're basically trying to change the convention used by EF Core, which means people working on your code base that are familiar with EF Core will either not use your variation or have to adjust to it. The gains of this seem minimal if there are any at all. – Xerillio Sep 25 '22 at 16:24
  • 1
    If you really want to build your own extension method, the documentation for [Extension Methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) is a good place to start. Please elaborate where you are stuck. – Xerillio Sep 25 '22 at 16:27

1 Answers1

1

That would work,I tried as you mentioned

enter image description here

but if you pass a wrong data type parameter into this method,you would get the error when you update database:

Column, parameter, or variable #2: Cannot find data type xx.
Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11
  • Thanks for your post. It works and the migration file contains the code line: Name = table.Column(type: "nvarchar(50)", nullable: false), – Thomas.Benz Oct 01 '22 at 19:17