1
I am developing a test project that I intend to include in my TCC, but I found a problem when relating two entities to create the Tablas in the database with the Entity Framework. Are they '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;
}
}
class Comment
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 various ways as presented in this post..: http://netcoders.com.br/mapeamento-com-entity-framework-code-first-fluent-api-parte-2/ Part 2.
But in every way I tried it creates the tables the way I need, and I can even insert the Post, but when I insert a comment it gives a relationship error.
Who can help me I thank.
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);
}
}
ENTERING 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
OBSERVATION
The database I’m using is Mysql.
What’s the mistake?....
– novic
Within the builder of
Comentario
you’re doing itPost = new Post();
. Take it off. The same goes for the builder ofPost
, take that lineCategoria = new Categoria();
. I’m not sure that’s it because I haven’t seen how you’re trying to add toDbContext
before calling theSaveChanges
, but try it there.– Alisson
I see a title like that question, it makes me want to suggest taking flowers next time.
– Oralista de Sistemas
Put the code you are using to save, and the error message.
– Cassio Alves
@Virgilionovic I edited the publication with new information and error.
– Eliseu Marcos
@Alisson I got to comment on these lines of code in the constructors and even then error occurred.
– Eliseu Marcos
@Cassioalves I put the code and the error message.
– Eliseu Marcos