TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ivan Climov
1.1k
692
22.4k
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
//
using
System.Collections.ObjectModel;
using
System.ComponentModel.DataAnnotations;
using
System.ComponentModel.DataAnnotations.Schema;
namespace
WpfAppFrm
{
[Table(
"Categories"
)]
public
class
Category
{
public
Category()
{
// this.Products = new ObservableCollection<Product>();
}
public
int
CategoryId {
get
;
set
; }
public
string
Name {
get
;
set
; }
// public virtual ObservableCollection<Product> Products { get; private set; }
}
}
ProductContext.cs
//
using
System.Data.Entity;
namespace
WpfAppFrm
{
public
class
ProductContext : DbContext
{
// public ProductContext() : base("DefaultConnection")
public
string
ConnectionString_test {
get
;
set
; }
public
ProductContext(
string
?onnectionString)
{
this
.ConnectionString_test = ?onnectionString;
}
public
DbSet<Category> Categories {
get
;
set
; }
// public DbSet<Product> Products { get; set; }
}
}
MainWindow.xaml.cs
//
using
System.ComponentModel;
using
System.Data.Entity;
namespace
WpfAppFrm
{
/// <summary>
/// ?????? ?????????????? ??? MainWindow.xaml
/// </summary>
public
partial
class
MainWindow : Window
{
// public static string ?onnectionString = @"C:\test\DB\NorthwindC.mdf";
public
static
string
?onnectionString = @
"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\test\DB\NorthwindC.mdf;Integrated Security=True;Connect Timeout=30"
;
// public static string ?onnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=WpfAppFrm.ProductContext;Integrated Security=True; MultipleActiveResultSets=True";
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;
// AddEntity();
// GetAll();
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
)
Reply
Answers (
4
)
Regarding .Net Project structure
How to make the treeview structure