Ivan Climov

Ivan Climov

  • 1.1k
  • 692
  • 22.6k

How to determine where the database with which the applicati

Feb 14 2020 2:20 PM
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

  1. //    
  2. using System.Collections.ObjectModel;   
  3. using System.ComponentModel.DataAnnotations;   
  4. using System.ComponentModel.DataAnnotations.Schema;   
  5.    
  6. namespace WpfAppFrm   
  7. {   
  8.     [Table("Categories")]   
  9.     public class Category   
  10.     {           
  11.         public Category()   
  12.         {   
  13.             // this.Products = new ObservableCollection<Product>();   
  14.         }   
  15.    
  16.         public int CategoryId { getset; }   
  17.         public string Name { getset; }   
  18.    
  19.         // public virtual ObservableCollection<Product> Products { get; private set; }   
  20.    
  21.     }   

ProductContext.cs
  1. //    
  2. using System.Data.Entity;   
  3.    
  4. namespace WpfAppFrm   
  5. {   
  6.     public class ProductContext : DbContext   
  7.     {   
  8.         // public ProductContext() : base("DefaultConnection")   
  9.         public string ConnectionString_test { getset; }   
  10.         public ProductContext(string ?onnectionString)   
  11.         {   
  12.             this.ConnectionString_test = ?onnectionString;   
  13.         }   
  14.    
  15.         public DbSet<Category> Categories { getset; }   
  16.         // public DbSet<Product> Products { get; set; }   
  17.     }   
  18. }
MainWindow.xaml.cs
  1. //   
  2. using System.ComponentModel;   
  3. using System.Data.Entity;   
  4.    
  5. namespace WpfAppFrm   
  6. {   
  7.     /// <summary>   
  8.     /// ?????? ?????????????? ??? MainWindow.xaml   
  9.     /// </summary>   
  10.     public partial class MainWindow : Window   
  11.     {   
  12.         // public static string ?onnectionString = @"C:\test\DB\NorthwindC.mdf";   
  13.         public static string ?onnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\test\DB\NorthwindC.mdf;Integrated Security=True;Connect Timeout=30";   
  14.         // public static string ?onnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=WpfAppFrm.ProductContext;Integrated Security=True;  MultipleActiveResultSets=True";   
  15.         private ProductContext _context = new ProductContext(?onnectionString);   
  16.    
  17.         public MainWindow()   
  18.         {   
  19.             InitializeComponent();   
  20.         }   
  21.    
  22.    
  23.         private void Window_Loaded(object sender, RoutedEventArgs e)   
  24.         {   
  25.             try   
  26.             {   
  27.                 System.Windows.Data.CollectionViewSource categoryViewSource =   
  28.                 ((System.Windows.Data.CollectionViewSource)(this.FindResource("categoryViewSource")));   
  29.    
  30.                 _context.Categories.Load();   
  31.    
  32.                 BindingList<Category> _studBindList = _context.Categories.Local.ToBindingList();   
  33.    
  34.                 categoryViewSource.Source = _context.Categories.Local;   
  35.    
  36.                 // AddEntity();   
  37.                 // GetAll();   
  38.                 string connectionString_str = _context.Database.Connection.ConnectionString;   
  39.                 string connectionString_str2 = _context.ConnectionString_test;   
  40.             }   
  41.             catch (Exception ex)   
  42.             {   
  43.                 throw;   
  44.             }   
  45.         }           
  46.    
  47.         public void AddEntity()   
  48.         {   
  49.             Category category = new Category   
  50.             {   
  51.                 Name = "Name_Category_4"   
  52.             };   
  53.    
  54.             _context.Categories.Add(category);   
  55.             _context.SaveChanges();   
  56.         }   
  57.    
  58.         public void GetAll()   
  59.         {   
  60.             _context.Categories.Load();   
  61.    
  62.             BindingList<Category> _studBindList = _context.Categories.Local.ToBindingList();   
  63.         }   
  64.     }   
  65. }
MainWindow.xaml
  1. <Window x:Class="WpfAppFrm.MainWindow"   
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  6.         xmlns:local="clr-namespace:WpfAppFrm"   
  7.         mc:Ignorable="d"   
  8.         Title="MainWindow" Height="450" Width="334.874" Loaded="Window_Loaded">   
  9.     <Window.Resources>   
  10.         <CollectionViewSource x:Key="categoryViewSource"   
  11.                                 d:DesignSource="{d:DesignInstance {x:Type local:Category}, CreateList=True}"/>   
  12.         <CollectionViewSource x:Key="categoryProductsViewSource" Source="{Binding Products, Source={StaticResource categoryViewSource}}"/>   
  13.     </Window.Resources>   
  14.     <Grid DataContext="{StaticResource categoryViewSource}" Margin="0,0,7,0">   
  15.         <Grid.ColumnDefinitions>   
  16.             <ColumnDefinition Width="33*"/>   
  17.             <ColumnDefinition Width="287*"/>   
  18.             <ColumnDefinition Width="0*"/>   
  19.             <ColumnDefinition Width="0*"/>   
  20.         </Grid.ColumnDefinitions>   
  21.         <DataGrid x:Name="categoryDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"   
  22.                     ItemsSource="{Binding}" Margin="13,13,3,257"   
  23.                     RowDetailsVisibilityMode="VisibleWhenSelected" Grid.ColumnSpan="2">   
  24.             <DataGrid.Columns>   
  25.                 <DataGridTextColumn x:Name="categoryIdColumn" Binding="{Binding CategoryId}"   
  26.                                     Header="CategoryId" Width="SizeToHeader"/>   
  27.                 <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}"   
  28.                                     Header="Name" Width="SizeToHeader"/>   
  29.             </DataGrid.Columns>   
  30.         </DataGrid>   
  31.    
  32.     </Grid>   
  33. </Window>   
TABLE [dbo].[Categories]
  1. CREATE TABLE [dbo].[Categories] (   
  2. [CategoryId] INT           IDENTITY (1, 1) NOT NULL,   
  3. [Name]       NVARCHAR (40) NULL,   
  4. PRIMARY KEY CLUSTERED ([CategoryId] ASC)    
 
 
 

Answers (4)