I will put the code of my three classes ... if someone could tell me what is my mistake i would be glad....
public class Autor
{
[Key]
public int AutorId { get; set; }
[Required]
[MaxLength(100)]
public string Nome { get; set; }
public virtual ICollection<Livro> Livros { get; set; }
public virtual ICollection<Avaliacao> Avaliacoes { get; set; }
}
public class Livro
{
[Key]
public int LivroId { get; set; }
[Required(ErrorMessage = "E necessario titulo")]
[MaxLength(100, ErrorMessage = "Titulo deve ter no maximo 100 caracteres")]
public string Titulo { get; set; }
public int AutorID { get; set; }
public virtual Autor Autor { get; set; }
public virtual ICollection<Avaliacao> Avaliacoes { get; set; }
}
}
public class Avaliacao
{
[Key]
public int AvaliacaoId{ get; set; }
[Required]
public string Nome { get; set; }
public int? LivroId { get; set; }
[Required]
public int AutorId { get; set; }
public virtual Livro Livro { get; set; }
public virtual Autor Autor { get; set; }
}
public class BibliotecaContext : DbContext
{
public DbSet<Autor> Autores { get; set; }
public DbSet<Livro> Livros { get; set; }
public DbSet<Avaliacao> Avaliacoes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
I get that error when i try to make the migration:
'FK_dbo.Avaliacao_dbo.Autor_AutorId' is not a constraint.
Could not drop constraint. See previous errors.
Relationships
Standard one to many:
modelBuilder.Entity<Product>()
.HasRequired(p => p.PrimaryCategory)
.WithMany(c => c.Products)
.HasForeignKey(p => p.PrimaryCategoryCode);
anderson7777
Member
141 Points
279 Posts
Entity Framework Code First Simple model not working....
Jan 31, 2013 04:47 AM|LINK
I will put the code of my three classes ... if someone could tell me what is my mistake i would be glad....
public class Autor { [Key] public int AutorId { get; set; } [Required] [MaxLength(100)] public string Nome { get; set; } public virtual ICollection<Livro> Livros { get; set; } public virtual ICollection<Avaliacao> Avaliacoes { get; set; } }public class Livro { [Key] public int LivroId { get; set; } [Required(ErrorMessage = "E necessario titulo")] [MaxLength(100, ErrorMessage = "Titulo deve ter no maximo 100 caracteres")] public string Titulo { get; set; } public int AutorID { get; set; } public virtual Autor Autor { get; set; } public virtual ICollection<Avaliacao> Avaliacoes { get; set; } } }public class Avaliacao { [Key] public int AvaliacaoId{ get; set; } [Required] public string Nome { get; set; } public int? LivroId { get; set; } [Required] public int AutorId { get; set; } public virtual Livro Livro { get; set; } public virtual Autor Autor { get; set; } }public class BibliotecaContext : DbContext { public DbSet<Autor> Autores { get; set; } public DbSet<Livro> Livros { get; set; } public DbSet<Avaliacao> Avaliacoes { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); }I get that error when i try to make the migration:
'FK_dbo.Avaliacao_dbo.Autor_AutorId' is not a constraint.
Could not drop constraint. See previous errors.
Does anyone know what shall i do?
thx in advance
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 04:56 AM|LINK
Apply dataannotation on the DAL's model is not the right way. I don't know why EF team sugguest this way.
http://martinfowler.com/eaaCatalog/index.html
Weera
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 04:58 AM|LINK
So you say i shouldnt use Code First?
asad.malik
Member
218 Points
103 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 05:02 AM|LINK
Hello,
It is better to use database first if you are not enough experienced, code first is a bit complicated when it comes to model relationships.
BTW have a look at this link it may solve your issue...
http://stackoverflow.com/questions/3496687/sql-server-error-is-not-a-constraint-could-not-drop-constraint-see-previo
Regards
Asad Malik
Mark as answer if it helps
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 05:03 AM|LINK
Did you do this? For example.
Relationships Standard one to many: modelBuilder.Entity<Product>() .HasRequired(p => p.PrimaryCategory) .WithMany(c => c.Products) .HasForeignKey(p => p.PrimaryCategoryCode);Weera
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 05:09 AM|LINK
thaicarrot;;;
still getting the same error....
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 05:14 AM|LINK
'FK_dbo.Avaliacao_dbo.Autor_AutorId' is not a constraint. Could not drop constraint. See previous errors.
Copy the stack trace here.
Weera
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 05:20 AM|LINK
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();Weera
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 05:29 AM|LINK
I dont know what is the Stack trace in this casee...
All i can add is the Migration file... Maybe the mistake is there
public partial class novoooo : DbMigration { public override void Up() { DropForeignKey("dbo.Avaliacao", "AutorId", "dbo.Autor"); DropIndex("dbo.Avaliacao", new[] { "AutorId" }); AlterColumn("dbo.Avaliacao", "AutorId", c => c.Int(nullable: false)); AddForeignKey("dbo.Avaliacao", "AutorId", "dbo.Autor", "AutorId", cascadeDelete: true); CreateIndex("dbo.Avaliacao", "AutorId"); } public override void Down() { DropIndex("dbo.Avaliacao", new[] { "AutorId" }); DropForeignKey("dbo.Avaliacao", "AutorId", "dbo.Autor"); AlterColumn("dbo.Avaliacao", "AutorId", c => c.Int()); CreateIndex("dbo.Avaliacao", "AutorId"); AddForeignKey("dbo.Avaliacao", "AutorId", "dbo.Autor", "AutorId"); } }Already tried some changes in the migration file but it didnt work
What does Visual Studio means by previous errors. Where can i find those previous errors?
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Jan 31, 2013 07:02 PM|LINK
please somebody help me....
I already had problems with Fluent API and now i have this...
I want to stay with Code First but if i cant solve this i will have to change to DataBase First and i know nothing of DB