Answer the question
In order to leave comments, you need to log in
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="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\DB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
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;
}
}
<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>
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
C:\Windows\System32>"C:\Program Files\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe" stop MSSQLLocalDB
DEattach if anything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question