I have three classes Autor, Avaliacao and Livro. I am having problems with the Autor-Avaliacao relationship so I am writing them down:
public class Avaliacao
{
[Key]
public int AvaliacaoId{ get; set; }
[Required]
public int AutorId { get; set; }
public virtual Autor Autor { get; set; }
public virtual Livro Livro { get; set; }
}
public class Autor
{
[Key]
public int AutorId { get; set; }
public virtual ICollection<Livro> Livros { get; set; }
public virtual ICollection<Avaliacao> Avaliacoes { get; set; }
}
The error that i get when I type add-migration is that:
Introducing FOREIGN KEY constraint 'FK_dbo.Avaliacao_dbo.Autor_Autor_AutorId' on table 'Avaliacao' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
I dont wanna change anything. I want the CascadeDelete to be set true. If i delete an Autor I want to delete its Avaliacao either.
Using the data annotation attributes with the ADO.NET Entity Framework affects how the ADO.NET Entity Framework generates the database schema and performs validation of values when the data is saved using
DbContext.SaveChanges. However, using the DbModelBuilder only changes the database schema. Which approach you choose can change the error messages you see when invalid data is submitted as well as determine whether a database call is made.
anderson7777
Member
141 Points
279 Posts
Error in Migration
Jan 29, 2013 12:16 AM|LINK
I have three classes Autor, Avaliacao and Livro. I am having problems with the Autor-Avaliacao relationship so I am writing them down:
public class Avaliacao { [Key] public int AvaliacaoId{ get; set; } [Required] public int AutorId { get; set; } public virtual Autor Autor { get; set; } public virtual Livro Livro { get; set; } }public class Autor { [Key] public int AutorId { get; set; } public virtual ICollection<Livro> Livros { get; set; } public virtual ICollection<Avaliacao> Avaliacoes { get; set; } }protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<Autor>().HasMany(p => p.Livros).WithRequired(p => p.Autor); modelBuilder.Entity<Autor>().HasMany(p => p.Avaliacoes).WithRequired(p => p.Autor); modelBuilder.Entity<Livro>().HasMany(p => p.Avaliacoes).WithRequired(p => p.Livro); }The error that i get when I type add-migration is that:
Introducing FOREIGN KEY constraint 'FK_dbo.Avaliacao_dbo.Autor_Autor_AutorId' on table 'Avaliacao' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
I dont wanna change anything. I want the CascadeDelete to be set true. If i delete an Autor I want to delete its Avaliacao either.
Can someone tell me what I am doing wrong?
thx
Anderson
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Error in Migration
Jan 29, 2013 05:10 AM|LINK
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); SetupAutorEntity(modelBuilder); SetupLivroEntity(modelBuilder); } private static void SetupAutorEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<Autor>().HasMany(p => p.Livros).WithRequired(p => p.Autor); modelBuilder.Entity<Autor>().HasMany(p => p.Avaliacoes).WithRequired(p => p.Autor); } private static void SetupLivroEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<Livro>().HasMany(p => p.Avaliacoes).WithRequired(p => p.Livro); }For example how to create casecade:
modelBuilder.Entity<Category>() .HasMany(c => c.Products) .WithRequired(p => p.PrimaryCategory) .HasForeignKey(p => p.PrimaryCategoryCode) .WillCascadeOnDelete(false);Why do you mix dataannotation with Fluent API?
Weera
anderson7777
Member
141 Points
279 Posts
Re: Error in Migration
Jan 29, 2013 05:50 PM|LINK
I make almost everything on Data Anotation but i have to describe the relashionships in Fluent API
anderson7777
Member
141 Points
279 Posts
Re: Error in Migration
Jan 29, 2013 08:53 PM|LINK
I dont want the Cascade Delete to bet set false.
I want the default behavior that deletes everything
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Error in Migration
Jan 29, 2013 09:35 PM|LINK
Just change the flag to true, then everything ok.
Using the data annotation attributes with the ADO.NET Entity Framework affects how the ADO.NET Entity Framework generates the database schema and performs validation of values when the data is saved using DbContext.SaveChanges. However, using the DbModelBuilder only changes the database schema. Which approach you choose can change the error messages you see when invalid data is submitted as well as determine whether a database call is made.
Weera
anderson7777
Member
141 Points
279 Posts
Re: Error in Migration
Jan 30, 2013 06:38 PM|LINK
One of the foregin keys must be nullable..
that was my mistake...
i will mark your answer and mine as answer because you tried to help