I am just starting out with my first Entity Framework project built from scratch, to teach myself. I have encountered EF projects before at work but they were already pre-built by the time I had come along so I just had to tweak and maintain them, never build a EF project from the ground up.
I have Googled this error and all I got were a bunch of very wordy StackOverflow posts that had link upon link and would've required me to spend the whole day reading the fricking encyclopedia for just a simple question.
I have a SQL Server Express instance named
CTI-BCH-W7\SQLEXPRESS
.
I am making a little CRM program to manage clients I tutor in math and science as a side business. I designed a DB with three tables:
Customers
,
Students
, and
Sessions
.
Then I built a
TutoringManagerObjects
DLL by using EF4.1 Database first. I used the proper NuGet packages and the DLL builds just fine.
Maybe I am getting something wrong in my app.config. Here is the app.config of my DLL (the layer that holds the entities):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<connectionStrings>
<add name="TutoringEntities" connectionString="metadata=res://*/TutoringModel.csdl|res://*/TutoringModel.ssdl|res://*/TutoringModel.msl;provider=System.Data.SqlClient;provider connection string="data source=CTI-BCH-W7\SQLEXPRESS;initial catalog=Tutoring;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
In my
TutoringManager.Context.cs
file (which is the generated code for the
TutoringManager.Context.tt
template), I have
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace TutoringManagerObjects
{
public partial class TutoringEntities : DbContext
{
public TutoringEntities()
: base("name=TutoringEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Session> Sessions { get; set; }
public DbSet<Student> Students { get; set; }
}
}
Then I made a Windows Forms application and used the Entities at the Data Source via Data menu -> Add New Data Source. Everything added OK and my objects added to the WinForms app just fine.
Now when I hit a Save button the code calls (I am *trying* to use the Model-View-Presenter pattern):
void buttonSave_Click(object sender, EventArgs e)
{
try
{
_presenter.SaveNewCustomer(new Customer
{
FirstName = firstNameTextBox.Text,
LastName = lastNameTextBox.Text,
Address1 = address1TextBox.Text,
Address2 = address2TextBox.Text,
City = cityTextBox.Text,
State = stateTextBox.Text,
ZIP = zIPTextBox.Text,
ContactPhone1 = contactPhone1TextBox.Text,
ContactPhone2 = contactPhone2TextBox.Text,
Email = emailTextBox.Text
});
}
catch (Exception exception)
{
MessageBox.Show(exception.Message,
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
}
In the Presenter, I do:
internal void SaveNewCustomer(Customer customerToSave)
{
if (customerToSave == null)
return;
using (var context = new TutoringEntities())
{
context.Customers.Add(customerToSave);
context.SaveChanges();
IsModified = false;
}
OnNewCustomerSaved();
}
When I click my Save button I get the message box
---------------------------
TutoringManager
---------------------------
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
---------------------------
OK
---------------------------
Here is the app.config of my WinForms app:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="TutoringDatabase"
connectionString="metadata=res://*/TutoringModel.csdl|res://*/TutoringModel.ssdl|res://*/TutoringModel.msl;provider=System.Data.SqlClient;provider connection string="data source=CTI-BCH-W7\SQLEXPRESS;initial catalog=Tutoring;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
<configSections>
<!-- For more information on Entity Framework configuration, visit http:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
Do I have something configured inproperly? What am I doing wrong? I am new to EntityFramework so I can't quite figure it out.