数据库连接C#WPF [英] Database connection C# WPF

查看:109
本文介绍了数据库连接C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DbContext表格中获得Nulls并感谢您的帮助。



型号:

I am getting Nulls in my DbContext tables and would be grateful for a little help.

Model:

[Table("tblCustomer")]
    public class Customer
    {
        [Key]
        public int CustomerID { get; set; }
        public virtual ICollection<bike> Bikes { get; set; }
        [Display(Name = "Customer")]
        public string CustomerFirstName { get; set; }
        [Display(Name = "Surname")]
        public string CustomerSurName { get; set; }
    }





DbContext:



DbContext:

public class BikesDbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }





App.Config中的ConnectionString:



ConnectionString in App.Config:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>
<connectionstrings>
    <add name="BikesDbContext" connectionString="server =(local)\SQLExpress;  AttachDbFilename=F:\Database\Bikes.mdf; database= db_Bikes; 
integrated security = SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>





调用代码:



Call in code:

BikesDbContext db = new BikesDbContext();

      var Dataset = db.Customers.Where
                     (x => x.CustomerSurName.StartsWith("S"));





当我在va设置断点时,我在db.Customers的调试中没有看到任何内容数据集线,并将感谢任何人可以给予的任何帮助。可以在Server explorer中看到数据库,并且表中的条目在Surname中以S开头。在我看来,DbContext并没有真正连接到数据库。



谢谢



< b>我尝试了什么:



我已经尝试了以上所有内容并且仍然从DbContext



I am not seeing anything in the debug in db.Customers when I set a breakpoint at the var Dataset line and would be grateful for any help that anyone can give. The Database can be seen in the Server explorer and has entries in the table that begin with "S" in the Surname. It does appear to me that the DbContext is not really connected to the database.

Thanks

What I have tried:

I have tried all the above and am still getting Nulls in the Table from DbContext

推荐答案

经过一些试验和错误后,我找到了错误。



首先,我没有从DbContext继承因此,我的上下文,我的上下文类现在看起来像这样



After some Trial and Error, I have managed to find the errors.

Firstly, I didn't inherit from DbContext in my Context, therefore, my context class now looks like this

public class BikesDbContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }





请注意我现在正确继承的区别。



另外,我发现我不需要在App.Config的连接字符串中使用AttachDbFilename



现在提供数据集在linq中填充了以S开头的客户



Notice the difference where I now correctly Inherit.

In addition, I found that I do not need to use the "AttachDbFilename" in the Connection String in the App.Config

This now provides a Dataset in the linq populated with customers starting with "S"


这篇关于数据库连接C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆