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

SQLite3 - StringLength Attribute Not Honored #124

Open
majew7 opened this issue Feb 8, 2024 · 1 comment
Open

SQLite3 - StringLength Attribute Not Honored #124

majew7 opened this issue Feb 8, 2024 · 1 comment

Comments

@majew7
Copy link

majew7 commented Feb 8, 2024

In Chapter 10 "Working with Data Using Entity Framework Core", I see Course.cs define Title with string length 60.

Course.cs

using System.ComponentModel.DataAnnotations;

namespace CoursesAndStudents;
public class Course
{
  public int CourseId { get; set; }

  [Required]
  [StringLength(60)]
  public string? Title { get; set; }

  public ICollection<Student>? Students { get; set; }
}

However, the generated SQL script appears to not honor StringLength attribute, nor MaxLength attribute, when building SQL for SQLite3.

Using C:\Code\Chapter10\CoursesAndStudents\bin\Debug\net6.0\Academy.db database file.
Database deleted: False
Database created: True
SQL script used to create database:
CREATE TABLE "Courses" (
    "CourseId" INTEGER NOT NULL CONSTRAINT "PK_Courses" PRIMARY KEY AUTOINCREMENT,
    "Title" TEXT NOT NULL
);

...

Q) Any thoughts, on why [StringLength(60)] is not applied to the schema?

Thanks in advance @markjprice!


Playing Around

Playing around I learned that the Column attribute does work however!

  [Required]
  [StringLength(60)]
  [Column(TypeName="text(50)")] // Works for SQLite
  public string? Title { get; set; }

Success:
image

@markjprice
Copy link
Owner

You're correct. In the next editions of my books, I will add a note about the difference between SQL Server and SQLite with Code First models.

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

2 participants