public class Livro
{
[Display(Name = "ISBN")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
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; }
[Required]
public int AutorID { get; set; }
public virtual Autor Autor { get; set; }
}
And what about the web.config? Am i adding the right conectionString?
namespace Biblioteca3.Models
{
public class Autor
{
[Key]
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 string testeeee { get; set; }
public ICollection<Livro> Livros { get; set; }
}
and My dbContext
public class BibliotecaContext : DbContext
{
public DbSet<Autor> Autores { get; set; }
public DbSet<Livro> Livros { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Autor>().HasMany(p => p.Livros).WithRequired(p => p.Autor);
}
Make sure that both tables have primary key on your database.
To check the connectionString, you can create a new project using "ASP.NET MVC Web template", and you can find a connectionString at Web.Config at the root of the project for your reference.
anderson7777
Member
141 Points
279 Posts
I am unable to create a controler
Jan 26, 2013 04:36 AM|LINK
Check this image and you will see what is happenning:
I dont know why its not working.
I already tried putting [Key] and didnt work either
Maybe its because i didnt add the right code to the web.config file. I dont know what to add. I tried this:
<connectionStrings> <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Biblioteca3-20130121022852;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Biblioteca3-20130121022852.mdf" /> </connectionStrings>Can somebody help me?
I use VisualStudio 2012 professional
thx
Anderson
jsiahaan
Contributor
2302 Points
585 Posts
Re: I am unable to create a controler
Jan 26, 2013 05:10 AM|LINK
Hi,
Try to rebuild your project and create a new controller again. By the way can you post your Livro class (or model)
Hope can help
Indonesian Humanitarian Foundation
sandeepsacha...
Member
68 Points
29 Posts
Re: I am unable to create a controler
Jan 26, 2013 05:41 AM|LINK
This is very common problem. But can be solved once you build your project and try again.
Website: www.sandeepsachan.com
anderson7777
Member
141 Points
279 Posts
Re: I am unable to create a controler
Jan 26, 2013 05:44 AM|LINK
I have already rebuilded and didnt work;.
Here is my livro class
public class Livro { [Display(Name = "ISBN")] [DatabaseGenerated(DatabaseGeneratedOption.None)] 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; } [Required] public int AutorID { get; set; } public virtual Autor Autor { get; set; } }And what about the web.config? Am i adding the right conectionString?
jsiahaan
Contributor
2302 Points
585 Posts
Re: I am unable to create a controler
Jan 26, 2013 08:43 AM|LINK
Hi,
Look back to Autor model and Livro, both need keys on your model or on the database. Could you please make keys for both of them.
The key can be (not always) like this:
[Key] public int AutorId { get; set; }Hope can help
Indonesian Humanitarian Foundation
anderson7777
Member
141 Points
279 Posts
Re: I am unable to create a controler
Jan 26, 2013 09:42 PM|LINK
still not working...
Here its my autor class
namespace Biblioteca3.Models { public class Autor { [Key] 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 string testeeee { get; set; } public ICollection<Livro> Livros { get; set; } }and My dbContext
public class BibliotecaContext : DbContext { public DbSet<Autor> Autores { get; set; } public DbSet<Livro> Livros { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<Autor>().HasMany(p => p.Livros).WithRequired(p => p.Autor); }jsiahaan
Contributor
2302 Points
585 Posts
Re: I am unable to create a controler
Jan 27, 2013 08:29 AM|LINK
Make sure that both tables have primary key on your database.
To check the connectionString, you can create a new project using "ASP.NET MVC Web template", and you can find a connectionString at Web.Config at the root of the project for your reference.
Hope can help
Indonesian Humanitarian Foundation