问题。
为什么应用程序不使用我的连接字符串?
使用者:
WPF框架。
目标。
使应用程序与数据库NORTHWINDC.MDF
一起使用。
说明。
我正在传递连接字符串
сonnectionStringStart = @" Data Source = (localdb)\mssqllocaldb; Initial Catalog = NORTHWINDC.MDF; Integrated Security = True; MultipleActiveResultSets = True ";
进入类的构造函数
class ProductContext: DbContext
。
我正在运行应用程序。
我得到一个连接字符串:
Data Source=(localdb)\mssqllocaldb;Initial Catalog=WpfAppFrm.ProductContext;Integrated Security=True;MultipleActiveResultSets=True
结果。
该应用程序创建数据库WpfAppFrm.ProductContext
,并且每次运行时都可以与数据库WpfAppFrm.ProductContext
一起使用。
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 сonnectionStringStart;
private ProductContext _context;
public MainWindow()
{
InitializeComponent();
сonnectionStringStart = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=NORTHWINDC.MDF;Integrated Security=True;MultipleActiveResultSets=True";
_context = new ProductContext(сonnectionStringStart);
}
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 сonnectionStringStartDuplicate = сonnectionStringStart;
string connectionStringResult1 = _context.Database.Connection.ConnectionString;
string connectionStringResult2 = _context.ConnectionString_test;
}
catch (Exception ex)
{
throw;
}
}
public void AddEntity()
{
Category category = new Category
{
Name = "Name_Category_4"
};
_context.Categories.Add(category);
string connectionString_str = _context.Database.Connection.ConnectionString;
_context.SaveChanges();
}
public void GetAll()
{
_context.Categories.Load();
BindingList<Category> _studBindList = _context.Categories.Local.ToBindingList();
}
}
}
答案 0 :(得分:2)
与其具有像此处存储连接字符串那样的属性
public string ConnectionString_test { get; set; }
public ProductContext(string сonnectionString)
{
this.ConnectionString_test = сonnectionString;
}
您需要将连接字符串传递给实际使用它的DbContext
的基本构造函数
public ProductContext(string сonnectionString) : base(connectionString)
{
}