using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Biblioteca4.DAL;
namespace Biblioteca4.Models
{
public class Autor
{
//[Key]
public int AutorId { get; set; }
[Required]
[MaxLength(100)]
public string Nome { get; set; }
[Required]
[Display(Name = "Data de nascimento")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime Data { get; set; }
public virtual ICollection<Livro> Livros { get; set; }
public virtual ICollection<Avaliacao> Avaliacoes { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Biblioteca4.Models
{
public class Avaliacao
{
//[Key]
public int AvaliacaoId{ get; set; }
[Required]
public string Nome { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime Data { get; set; }
[Required]
public string text { get; set; }
[Range(0, 5, ErrorMessage = "Nota deve estar entre 0 e 5")]
public int Nota { get; set; }
//public int LivroId { get; set; }
public int AutorId { get; set; }
//public virtual Livro Livro { get; set; }
public virtual Autor Autor { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Biblioteca4.DAL;
namespace Biblioteca4.Models
{
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; }
[Required]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime Data { get; set; }
[Required]
[Range(1, 5000, ErrorMessage = "Valor deve ser entre 1 e 5000")]
public int Paginas { get; set; }
public int AutorID { get; set; }
public virtual Autor Autor { get; set; }
public decimal Price { get; set; }
//public virtual ICollection<Avaliacao> Avaliacoes { get; set; }
}
}
SQL Server does simple counting of cascade paths and, rather than trying to work out whether any cycles actually exist, it assumes the worst and refuses to create the referential actions (CASCADE): you can and should still create the constraints without
the referential actions. If you can't alter your design (or doing so would compromise things) then you should consider using triggers as a last resort.
FWIW resolving cascade paths is a complex problem. Other SQL products will simply ignore the problem and allow you to create cycles, in which case it will be a race to see which will overwrite the value last, probably to the ignorance of the designer (e.g.
ACE/Jet does this). I understand some SQL products will attempt to resolve simple cases. Fact remains, SQL Server doesn't even try, plays it ultra safe by disallowing more than one path and at least it tells you so.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Biblioteca4.Models
{
public class Avaliacao
{
//[Key]
public int AvaliacaoId{ get; set; }
[Required]
public string Nome { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime Data { get; set; }
[Required]
public string text { get; set; }
[Range(0, 5, ErrorMessage = "Nota deve estar entre 0 e 5")]
public int Nota { get; set; }
public int LivroId { get; set; } you can use the Independeny Navigation FK, when you query the data by using funxtion such as the Join etc
public int AutorId { get; set; }
//public virtual Livro Livro { get; set; }
public virtual Autor Autor { get; set; }
}
}
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 03:34 AM|LINK
You cannot create the Relationship between the Livro and Avaliacao.
That is the Problem. You database is wrong.
Weera
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 03:36 AM|LINK
why not?
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 03:41 AM|LINK
Following is new code
using System; using System.Collections.Generic; using System.Data.Entity; using Biblioteca4.Models; using System.Data.Entity.ModelConfiguration.Conventions; using System.ComponentModel.DataAnnotations.Schema; namespace Biblioteca4.DAL { 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>(); SetupAutorEntity(modelBuilder); SetupLivroEntity(modelBuilder); SetupAliacaoEntity(modelBuilder); } private static void SetupAutorEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<Autor>().HasKey(a => a.AutorId); modelBuilder.Entity<Autor>().Property(a => a.AutorId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); } private static void SetupAliacaoEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<Avaliacao>().HasKey(a => a.AvaliacaoId); modelBuilder.Entity<Avaliacao>().Property(a => a.AvaliacaoId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); } private static void SetupLivroEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<Livro>().HasKey(l => l.LivroId); modelBuilder.Entity<Livro>().Property(l => l.LivroId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); modelBuilder.Entity<Livro>().HasRequired(l => l.Autor).WithMany(o => o.Livros).HasForeignKey(d => d.AutorID); } } }Weera
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 03:42 AM|LINK
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using Biblioteca4.DAL; namespace Biblioteca4.Models { public class Autor { //[Key] public int AutorId { get; set; } [Required] [MaxLength(100)] public string Nome { get; set; } [Required] [Display(Name = "Data de nascimento")] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] public DateTime Data { get; set; } public virtual ICollection<Livro> Livros { get; set; } public virtual ICollection<Avaliacao> Avaliacoes { get; set; } } }using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Biblioteca4.Models { public class Avaliacao { //[Key] public int AvaliacaoId{ get; set; } [Required] public string Nome { get; set; } [Required] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] public DateTime Data { get; set; } [Required] public string text { get; set; } [Range(0, 5, ErrorMessage = "Nota deve estar entre 0 e 5")] public int Nota { get; set; } //public int LivroId { get; set; } public int AutorId { get; set; } //public virtual Livro Livro { get; set; } public virtual Autor Autor { get; set; } } }using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using Biblioteca4.DAL; namespace Biblioteca4.Models { 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; } [Required] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] public DateTime Data { get; set; } [Required] [Range(1, 5000, ErrorMessage = "Valor deve ser entre 1 e 5000")] public int Paginas { get; set; } public int AutorID { get; set; } public virtual Autor Autor { get; set; } public decimal Price { get; set; } //public virtual ICollection<Avaliacao> Avaliacoes { get; set; } } }Weera
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 03:46 AM|LINK
SQL Server does simple counting of cascade paths and, rather than trying to work out whether any cycles actually exist, it assumes the worst and refuses to create the referential actions (CASCADE): you can and should still create the constraints without the referential actions. If you can't alter your design (or doing so would compromise things) then you should consider using triggers as a last resort.
FWIW resolving cascade paths is a complex problem. Other SQL products will simply ignore the problem and allow you to create cycles, in which case it will be a race to see which will overwrite the value last, probably to the ignorance of the designer (e.g. ACE/Jet does this). I understand some SQL products will attempt to resolve simple cases. Fact remains, SQL Server doesn't even try, plays it ultra safe by disallowing more than one path and at least it tells you so.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Biblioteca4.Models { public class Avaliacao { //[Key] public int AvaliacaoId{ get; set; } [Required] public string Nome { get; set; } [Required] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] public DateTime Data { get; set; } [Required] public string text { get; set; } [Range(0, 5, ErrorMessage = "Nota deve estar entre 0 e 5")] public int Nota { get; set; } public int LivroId { get; set; } you can use the Independeny Navigation FK, when you query the data by using funxtion such as the Join etc public int AutorId { get; set; } //public virtual Livro Livro { get; set; } public virtual Autor Autor { get; set; } } }Weera
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 06:40 PM|LINK
I tried your code and stil dont work.
Did your code worked there?
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 07:22 PM|LINK
Yes, it is working.
Weera
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 07:57 PM|LINK
now it worked...
But the Livro entity doesnt have Avaliacoes Colection...
I know i can find using a query but having the navigation property is very important feature...
I had to make a new Project to make it work...
It seems that Migration has some kind of memory, And sometimes previous erros can affect some late Migration.. I think that its the problem sometimes
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 08:17 PM|LINK
Why we use code first? We use it when we code because of it is more friendly when code. However, We should not use it for Product.
The SQL has lots advantages that the code first doesn't.
Consider from you model it also doesn't work with SQL tool because of the Model conflict. You have nochoices eccept re-design you model.
Weera
anderson7777
Member
141 Points
279 Posts
Re: Entity Framework Code First Simple model not working....
Feb 01, 2013 09:26 PM|LINK
Yeah...
Code First is giving me a lot of problems...
thx for your help and your time I will try Database first...
Do you know other people having problem with Code First or is it just me?