Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

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:

C#
internal void SaveNewCustomer(Customer customerToSave)
{
    if (customerToSave == null)
        return;

    using (var context = new TutoringEntities())
    {
        context.Customers.Add(customerToSave);
        context.SaveChanges();
        IsModified = false; // turn dirty bit off now that we've saved
    }

    OnNewCustomerSaved();   // inform UI
}


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://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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.
Posted
Updated 20-Sep-12 7:08am
v2

1 solution

My solution was found in the post here:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ab2dd9f9-1eda-4d4e-a0c0-d1f1964343f7[^]

It says to put the <connectionstring></connectionstring> section after the <configsection></configsection> section and I also renamed the connection string to match the name of the auto-generated file:

XML
<configuration>
  <configsections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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"/>
  </add></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>


Now it worked! I almost had a heart attack when it happened! Ha ha ha ha....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900