1

I'm trying to do a simple data insertion on my database, but I'm getting the follow error: "Invalid Object Name dbo.".

Import(I think) detail... I did basicaly the same code in another test, but I created the table and the db with Sql Management Studio. Now, I just have created in the visual studio using an Empty EF Designer model.

My Insertion code:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        using (AlunoModelContainer ctx = new AlunoModelContainer()) { 
            tb_alunos student = new tb_alunos();

        student.nome_aluno = textBox1.Text;
        student.idade_aluno = textBox2.Text;
        student.curso_aluno = textBox4.Text;
        student.endereco_aluno = textBox5.Text;

        ctx.Alunos.Add(student);
        ctx.SaveChanges();
        MessageBox.Show("Estudante cadastrado com sucesso");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Usuário não pôde ser cadastrado." + ex.ToString());
    }

}

My db context code:

namespace Sistema_Alunos
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class AlunoModelContainer : DbContext
    {
        public AlunoModelContainer()
            : base("name=AlunoModelContainer")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<tb_alunos> Alunos { get; set; }
    }
}

And a simple image of my folder structure:

enter image description here

3
  • Which table name doesn't exist (on single table or all tables inside DB) and which line throwing error? I see you're attempted Code First Migrations, it may related to table relationships or somewhat called 'pluralization'. Commented Apr 25, 2017 at 6:18
  • Put error details which you get and moment when you get it. Also add entity model view. Commented Apr 25, 2017 at 6:31
  • Hi friend, today i have got the same error. Table successfully added to database using EF Code First, now I am trying to populate my table but got this same error. Plz reply if you found solution. Commented Jul 3, 2018 at 18:02

3 Answers 3

1

Try adding this line to your code.

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer - no need to change names
0

Your partial class tb_alunos does not have [Table("tb_alunos")] attribute so it cannot be mapped to the table of the database .

Try this :

 [Table("tb_alunos")]
public partial class tb_alunos
{
    public int alunoID { get; set; }
    public string nome_aluno { get; set; }
    public string curso_aluno { get; set; }
    public string endereco_aluno { get; set; }
    public string idade__aluno { get; set; }

}

1 Comment

in EF we only need to add table attribute if out table name is different in Database. So i don't think this is needed.
0

I found the solution for this problem. I have got same problem. I have created table called SubscriptionType but while creating table by convention Entity Framework added extra "S" at the end of table name and it became SubscriptionTypes.

So match your table name in database and in your model class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.