A
A
Alexey Pavlov2016-06-15 21:05:49
WPF
Alexey Pavlov, 2016-06-15 21:05:49

How to use LocalDb database with ADO.NET EDM in WPF project?

1) I create a new WPF project in Visual Studio Community 2015 (Update 2).
2) Install EntityFramework 6.1.3 via NuGet.
3.1) I add an EDM element, type from the database
3.2) I create a new database "DB", specifying the file in the root of the project (where App.config is), check the box to save the connection settings in App.config
3.3) I include foreign keys (there are no tables yet , but still I put a tick)
3.4) I create a table Test with the fields Id and Name, I update it.
3.5) I add a couple of data records.
3.6) In edmx I update the model from the database, save it. The Test class will be created.
3.7) App.config automatically configured connectionStrings

<connectionStrings>
    <add name="DBEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\DB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

4) I create the MainVm.cs class (at the root of the project):
using System.Collections.ObjectModel;

namespace DbTest
{
    public class MainVm
    {
        private static readonly DBEntities Entities = new DBEntities();

        public ObservableCollection<Test> Tests { get; set; } = Entities.Test.Local;
    }
}

5) I update MainWindow.xaml:
<Window x:Class="DbTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DbTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainVm/>
    </Window.DataContext>
    <Grid>
        <DataGrid ItemsSource="{Binding Tests}"/>
    </Grid>
</Window>

In theory, everything. A table with data should be shown. What did I do wrong or did I forget something?
The list of errors says
No connection string named 'DBEntities' could be found in the application config file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#algooptimize #bottize, 2016-06-16
@user004

C:\Windows\System32>"C:\Program Files\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe" stop MSSQLLocalDB
DEattach if anything.

R
Roman, 2016-06-16
@yarosroman

private static readonly DBEntities Entities = new DBEntities( database connection string );
connection string like "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\base.mdf;Integrated Security=True"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question