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:
