I'm developing a test project that I want to include in my TCC, but I encountered a problem matching two entities to create the tables in the database with the Entity Framework. They are 'Post' and 'Comment'
public class Post
{
public Post()
{
DataPost = DateTime.Now;
Categoria = new Categoria();
Comentarios = new List<Comentario>();
}
public int PostId { get; set; }
public string TituloPost { get; set; }
public string SubTituloPost { get; set; }
public DateTime DataPost { get; set; }
//Post obrigatoriamente tem uma categoria
public virtual Categoria Categoria { get; set; }
//Post tem 0 ou vários comentários
public virtual ICollection<Comentario> Comentarios { get; set; } //=====Comentários
public override string ToString()
{
return TituloPost;
}
}
Comment class
public class Comentario
{
public Comentario()
{
DataComentario = DateTime.Now;
Post = new Post();
}
public int ComentarioId { get; set; }
public DateTime DataComentario { get; set; }
public string DescricaoComentario { get; set; }
//1 Cometário pertence somente a 1 Post
public virtual Post Post { get; set; } //=============Post
public override string ToString()
{
return DescricaoComentario;
}
}
Category class
public class Categoria
{
public Categoria()
{
Ativa = true;
Posts = new List<Post>();
}
public int CategoriaId { get; set; }
public string DescricaoCategoria { get; set; }
public bool Ativa { get; set; }
//Categoria tem uma coleção de Post
public virtual ICollection<Post> Posts { get; set; }
public override string ToString()
{
return DescricaoCategoria;
}
}
I tried to do the mapping in several ways as presented in this post ..: link Part 2.
But in all ways I've tried it, it creates the tables the way I need it, and I can even insert the Post, but when I insert a comment it gives a relationship error.
Who can help me, I thank you.
Edit
MAPPING
public class MapPost : EntityTypeConfiguration<Post>
{
public MapPost()
{
ToTable("Post");
HasKey(x => x.PostId);
Property(x => x.TituloPost).HasMaxLength(150).IsRequired();
Property(x => x.SubTituloPost).HasMaxLength(300).IsRequired();
Property(x => x.DataPost).IsRequired();
HasRequired(p => p.Categoria)
.WithMany(c => c.Posts)
.Map(c => c.MapKey("CategoriaId"));
}
}
public class MapCategoria : EntityTypeConfiguration<Categoria>
{
public MapCategoria()
{
ToTable("Categoria");
HasKey(i => i.CategoriaId);
Property(d => d.DescricaoCategoria).HasMaxLength(60).IsRequired();
}
}
public class MapComentario : EntityTypeConfiguration<Comentario>
{
public MapComentario()
{
ToTable("Comentario");
HasKey(i => i.ComentarioId);
Property(d => d.DataComentario).IsRequired();
Property(d => d.DescricaoComentario).IsRequired();
HasRequired(x => x.Post)
.WithMany(c => c.Comentarios)
.Map(x => x.MapKey("fk_PostId"));
}
}
CONTEXT
public class Context : DbContext
{
public Context() : base ("ConnectionString")
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
public DbSet<Categoria> Categoria { get; set; }
public DbSet<Post> Post { get; set; }
public DbSet<Comentario> Comentario { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Properties<string>().Configure(p => p.HasColumnType("varchar"));
modelBuilder.Configurations.Add(new Map.MapPost());
modelBuilder.Configurations.Add(new Map.MapCategoria());
modelBuilder.Configurations.Add(new Map.MapComentario());
base.OnModelCreating(modelBuilder);
}
}
INSERTING DATA
var post = new Post
{
TituloPost = "Titulo Post 02",
SubTituloPost = "SubTitulo Post 02",
Categoria = new Categoria
{
DescricaoCategoria = "Categoria de Teste 02",
Ativa = true,
},
Comentarios = new List<Comentario>
{
new Comentario
{
DescricaoComentario = "Comentário Teste 01",
DataComentario = DateTime.Now
},
new Comentario
{
DescricaoComentario = "Comentário Teste 02",
DataComentario = DateTime.Now
},
new Comentario
{
DescricaoComentario = "Comentário Teste 03",
DataComentario = DateTime.Now
}
}
};
db.Post.Add(post);
if (!db.GetValidationErrors().Any())
{
db.SaveChanges();
}
ERROR
OBERVAÇÃO
The database I'm using is MySql.