C
C#4w ago
kianyuen

Migration not reflecting Model correctly

Context: I'm using .NET core 6.0, ASP.NET MVC, trying to initiate the db with Migrations Got this error:
PostgresException: 23502: null value in column "parent_id" of relation "post" violates not-null constraint DETAIL: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
PostgresException: 23502: null value in column "parent_id" of relation "post" violates not-null constraint DETAIL: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
I clearly defined the ParentId to be nullable here:
public class Post
{
public int Id { get; set; }
public PostType Type { get; set; }
public int? ParentId { get; set; }
public virtual Post Parent { get; set; }
[InverseProperty("Parent")]
public virtual ICollection<Post> Answers { get; set; }
public int? AcceptedAnswerId { get; set; }
public virtual Post AcceptedAnswer { get; set; }
public int UserId { get; set; }
public User User { get; set; }
[StringLength(255)]
public string Title { get; set; }
[Column(TypeName = "text")]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public Post()
{
Tags = new HashSet<Tag>();
Answers = new HashSet<Post>();
}
}
public class Post
{
public int Id { get; set; }
public PostType Type { get; set; }
public int? ParentId { get; set; }
public virtual Post Parent { get; set; }
[InverseProperty("Parent")]
public virtual ICollection<Post> Answers { get; set; }
public int? AcceptedAnswerId { get; set; }
public virtual Post AcceptedAnswer { get; set; }
public int UserId { get; set; }
public User User { get; set; }
[StringLength(255)]
public string Title { get; set; }
[Column(TypeName = "text")]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public Post()
{
Tags = new HashSet<Tag>();
Answers = new HashSet<Post>();
}
}
Any suggestion on how I can fix this? I think I can just modify the generated migration file, but I'm afraid that would break the migrations.
4 Replies
kianyuen
kianyuenOP4w ago
Tried [AllowNull] on the ParentId field. Didnt work
ero
ero4w ago
It's telling you that the id must not be nullable
kianyuen
kianyuenOP4w ago
Yeah, but I want it to be nullable, it's just a foreign key Found out that it's a default setting in .csproj
kianyuen
kianyuenOP4w ago
Stack Overflow
String is not nullable by default in Entity Framework Core 6.0
I was creating a database table in Entity Framework Core 6.0. I was using code first approach in my project. There was a string type property in TestModel named Address. using System.ComponentModel.
Want results from more Discord servers?
Add your server