C# 如何连接到mdf数据库?

C# 如何连接到mdf数据库?,c#,wpf,entity-framework,C#,Wpf,Entity Framework,使用人: WPF框架 实体框架 我得到\u studBindList.Count=0 如何连接到mdf数据库 Category.cs using System.ComponentModel.DataAnnotations.Schema; namespace WpfAppFrm { [Table("Categories")] public class Category { public Category() {

使用人:

  • WPF框架
  • 实体框架
我得到
\u studBindList.Count=0

如何连接到mdf数据库

Category.cs

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; }

    }
}
using System.Data.Entity;

namespace WpfAppFrm
{
    public class ProductContext : DbContext
    {
        // public ProductContext() : base("DefaultConnection")
        public ProductContext(string сonnectionString)
        {

        }

        public DbSet<Category> Categories { get; set; }
        // public DbSet<Product> Products { get; set; }
    }
}
//
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";
        private ProductContext _context = new ProductContext(сonnectionString);

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(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;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
Picture-1


更新-1 Picture-2


更新-2 新增方法:

  • 加法()
  • GetAll()
方法加法()

 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();
        }
方法GetAll()

 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();
        }
public void GetAll()
{
_context.Categories.Load();
BindingList _studBindList=_context.Categories.Local.ToBindingList();
}
我运行了两次调试。
当我第二次调试时,我拍了一张照片

Picture-3

.mdf是SQL Server使用的一种文件格式。您不能直接连接到这样的文件。相反,您需要将该文件作为数据库导入SQL Server(如果您没有SQL Server,您也必须安装它)。

LocalDB
<代码>C:\Program Files\Microsoft SQL Server\130\LocalDB\Binn本地数据库是SQL Server Express的一项功能。这样就行了,是的。从技术上讲,本地数据库是sql server的一个版本。是什么让您认为数据库表中有任何条目?你向那个数据库添加了什么吗?@mm8 1。请参见
Picture-1
。2.总的来说,我的代码是正确的吗?图片并没有说太多。您真的在查询C:\test\DB\NorthwindC.mdf而不是某个本地副本吗?@mm8 1。请参见
Update-1
。我能回答你的问题吗?2.一般来说,我的代码应该工作吗?尝试以编程方式添加一个新实体,然后加载所有实体。那么你要么得到一个错误,要么找到它。
public void GetAll()
        {
            _context.Categories.Load();

            BindingList<Category> _studBindList = _context.Categories.Local.ToBindingList();
        }