实体框架中的SQL连接(WPF MVVM)

实体框架中的SQL连接(WPF MVVM),sql,wpf,entity-framework,mvvm,ef-code-first,Sql,Wpf,Entity Framework,Mvvm,Ef Code First,我想使用实体框架与原始sql进行简单连接 我的模型类定义如下: public class Price { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id {get; private set;} public int ProductId {get; set;} public double Price {get; set;} public virtual

我想使用实体框架与原始sql进行简单连接

我的模型类定义如下:

public class Price
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; private set;}
    public int ProductId {get; set;}
    public double Price {get; set;}
    public virtual Product Product {get; set;}
}
    public class Product
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; private set;}
    public string Name {get; set;}
    public ICollection<Price> Price {get; set;}
}
public class PriceContext : DbContext
{
public DbSet<Price> Prices {get; set;}
public DbSet<Product> Products {get; set;}

public PriceContext() : base("ConnectionString") {}

public List<Price> FindAll()
{
    string sqlQuery= @"select products.name, prices.[type], prices.[from], prices.price, prices.pricelistname  from products, prices where prices.productid = products.id;

    return Prices.SqlQuery(sqlQuery).ToList<Price>();
}
}
public void FindAll()
    {
        var context = new PriceContext();
        var prices = context.FindAll();
        ObservablePrices = new ObservableCollection<Price>(prices);
    }
公共类价格
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
public int Id{get;private set;}
public int ProductId{get;set;}
公共双价{get;set;}
公共虚拟产品产品{get;set;}
}
公共类产品
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
public int Id{get;private set;}
公共字符串名称{get;set;}
公共ICollection价格{get;set;}
}
我的PriceContext类如下所示:

public class Price
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; private set;}
    public int ProductId {get; set;}
    public double Price {get; set;}
    public virtual Product Product {get; set;}
}
    public class Product
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; private set;}
    public string Name {get; set;}
    public ICollection<Price> Price {get; set;}
}
public class PriceContext : DbContext
{
public DbSet<Price> Prices {get; set;}
public DbSet<Product> Products {get; set;}

public PriceContext() : base("ConnectionString") {}

public List<Price> FindAll()
{
    string sqlQuery= @"select products.name, prices.[type], prices.[from], prices.price, prices.pricelistname  from products, prices where prices.productid = products.id;

    return Prices.SqlQuery(sqlQuery).ToList<Price>();
}
}
public void FindAll()
    {
        var context = new PriceContext();
        var prices = context.FindAll();
        ObservablePrices = new ObservableCollection<Price>(prices);
    }
公共类PriceContext:DbContext
{
公共数据库集价格{get;set;}
公共数据库集产品{get;set;}
public PriceContext():base(“ConnectionString”){}
公共列表FindAll()
{
string sqlQuery=@“选择products.name,prices.[type],prices.[from],prices.price,prices.prices.pricelistname from products,prices where prices.productid=products.id;
returnprices.SqlQuery(SqlQuery.ToList();
}
}
我的ViewModel有如下方法:

public class Price
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; private set;}
    public int ProductId {get; set;}
    public double Price {get; set;}
    public virtual Product Product {get; set;}
}
    public class Product
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; private set;}
    public string Name {get; set;}
    public ICollection<Price> Price {get; set;}
}
public class PriceContext : DbContext
{
public DbSet<Price> Prices {get; set;}
public DbSet<Product> Products {get; set;}

public PriceContext() : base("ConnectionString") {}

public List<Price> FindAll()
{
    string sqlQuery= @"select products.name, prices.[type], prices.[from], prices.price, prices.pricelistname  from products, prices where prices.productid = products.id;

    return Prices.SqlQuery(sqlQuery).ToList<Price>();
}
}
public void FindAll()
    {
        var context = new PriceContext();
        var prices = context.FindAll();
        ObservablePrices = new ObservableCollection<Price>(prices);
    }
public void FindAll()
{
var context=new PriceContext();
var prices=context.FindAll();
ObservablePrices=新的ObservableCollection(价格);
}
我的WPF视图在加载时运行
ViewModel.FindAll()
,并且有一个
DataGrid
元素绑定到
observedprices


查询
sqlQuery
似乎不起作用。但是,如果我将其更改为
select*from prices
,它工作得很好(只是为了查看我的所有绑定等都按预期工作)。我似乎无法在此上下文中查询表
products

删除了DbContext中的方法,并采用了类型列表的方法。

pública static List<Price> FindAll()
{
    using (var context = new PriceContext())
    {
          var sqlQuery = from a in context.products
                         join b in context.prices
                         on a.id equals b.productid
                         select a;
    }
    return sqlQuery;
}
pública静态列表FindAll()
{
使用(var context=new PriceContext())
{
var sqlQuery=来自context.products中的
在上下文中加入b
a.id等于b.productid
选择一个;
}
返回sqlQuery;
}

对于更复杂的联合交易,您不应该每次都重置可观察价格。您只需要用您的项目填充即可: