How to determine where the database with which the application works is located?
I launch the application.
The application adds and receives entries.
The application adds and receives records to another database, and not to which the connection is made in the code.
I can’t understand what database the application works with.
Category.cs
-
- using System.Collections.ObjectModel;
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
-
- namespace WpfAppFrm
- {
- [Table("Categories")]
- public class Category
- {
- public Category()
- {
-
- }
-
- public int CategoryId { get; set; }
- public string Name { get; set; }
-
-
-
- }
- }
ProductContext.cs
-
- using System.Data.Entity;
-
- namespace WpfAppFrm
- {
- public class ProductContext : DbContext
- {
-
- public string ConnectionString_test { get; set; }
- public ProductContext(string ?onnectionString)
- {
- this.ConnectionString_test = ?onnectionString;
- }
-
- public DbSet<Category> Categories { get; set; }
-
- }
- }
MainWindow.xaml.cs
-
- using System.ComponentModel;
- using System.Data.Entity;
-
- namespace WpfAppFrm
- {
-
-
-
- public partial class MainWindow : Window
- {
-
- public static string ?onnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\test\DB\NorthwindC.mdf;Integrated Security=True;Connect Timeout=30";
-
- private ProductContext _context = new ProductContext(?onnectionString);
-
- public MainWindow()
- {
- InitializeComponent();
- }
-
-
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- try
- {
- System.Windows.Data.CollectionViewSource categoryViewSource =
- ((System.Windows.Data.CollectionViewSource)(this.FindResource("categoryViewSource")));
-
- _context.Categories.Load();
-
- BindingList<Category> _studBindList = _context.Categories.Local.ToBindingList();
-
- categoryViewSource.Source = _context.Categories.Local;
-
-
-
- string connectionString_str = _context.Database.Connection.ConnectionString;
- string connectionString_str2 = _context.ConnectionString_test;
- }
- catch (Exception ex)
- {
- throw;
- }
- }
-
- public void AddEntity()
- {
- Category category = new Category
- {
- Name = "Name_Category_4"
- };
-
- _context.Categories.Add(category);
- _context.SaveChanges();
- }
-
- public void GetAll()
- {
- _context.Categories.Load();
-
- BindingList<Category> _studBindList = _context.Categories.Local.ToBindingList();
- }
- }
- }
MainWindow.xaml
- <Window x:Class="WpfAppFrm.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:WpfAppFrm"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="334.874" Loaded="Window_Loaded">
- <Window.Resources>
- <CollectionViewSource x:Key="categoryViewSource"
- d:DesignSource="{d:DesignInstance {x:Type local:Category}, CreateList=True}"/>
- <CollectionViewSource x:Key="categoryProductsViewSource" Source="{Binding Products, Source={StaticResource categoryViewSource}}"/>
- </Window.Resources>
- <Grid DataContext="{StaticResource categoryViewSource}" Margin="0,0,7,0">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="33*"/>
- <ColumnDefinition Width="287*"/>
- <ColumnDefinition Width="0*"/>
- <ColumnDefinition Width="0*"/>
- </Grid.ColumnDefinitions>
- <DataGrid x:Name="categoryDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"
- ItemsSource="{Binding}" Margin="13,13,3,257"
- RowDetailsVisibilityMode="VisibleWhenSelected" Grid.ColumnSpan="2">
- <DataGrid.Columns>
- <DataGridTextColumn x:Name="categoryIdColumn" Binding="{Binding CategoryId}"
- Header="CategoryId" Width="SizeToHeader"/>
- <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}"
- Header="Name" Width="SizeToHeader"/>
- </DataGrid.Columns>
- </DataGrid>
-
- </Grid>
- </Window>
TABLE [dbo].[Categories]
- CREATE TABLE [dbo].[Categories] (
- [CategoryId] INT IDENTITY (1, 1) NOT NULL,
- [Name] NVARCHAR (40) NULL,
- PRIMARY KEY CLUSTERED ([CategoryId] ASC)