本文介绍了我如何获得的EntityFramework检查2参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个相关的对象(非相关属性不再赘述)

I have 3 related objects (non relevant properties omitted for brevity):

public class Product
{
    public int ID { get; set; }
    public virtual ProductPrice Price { get; set; }
}

public class ProductPrice
{
    public int     ID         { get; set; }
    public int     ProductID  { get; set; }
    public int     VerticalID { get; set; }
    public decimal Value      { get; set; }

    public virtual Product  Product  { get; set; }
    public virtual Vertical Vertical { get; set; }

    public override string ToString()
    {
        return Value.ToString("C");
    }
}

public class Vertical
{
    public int ID      { get; set; }
    public string Name { get; set; }
}

产品价​​格变化的基础上目前的垂直。当前的垂直可能(最终)被存储在会话,但其时,让我们假设,这将是一个查询字符串参数。 (例如 mydomain.com?VerticalID=2 )。

我的提问

当用户访问 mydomain.com/products?VerticalID=2 mydomain.com/products/?VerticalID=2 我如何能得到实体框架来选择/分配基础上的ProductID 的VerticalID正确的价格 - 使这成为可能:?

When a user visits mydomain.com/products?VerticalID=2 or mydomain.com/products/?VerticalID=2 how can I get the Entity Framework to select/assign the correct price based on the ProductID and the VerticalID - making this possible?:

@Model.Price.ToString()


更新1(样本数据和DB结构)

下面是我用虚拟内容表:


Update 1 (sample data and DB structure)

Here are my tables with dummy content:

产品

ProductPrices

垂直行业

应该有一个价格,每个产品,每个垂直。查询看起来是这样的:

There should be one price, per product, per vertical. The query would look something like:

-- Let's assume ProductID = 2 and VerticalID = 1 (e.g. mydomain.com/products/2?VerticalID=1)
SELECT * FROM ProductPrices WHERE ProductID = 2 AND VerticalID = 1

上面的查询将返回1行(这是它应该总是返回)

The above query would return 1 row (which is what it should always return)

有关说明目的我增加了 VerticalID 属性产品:

For illustrative purposes I added the VerticalID property to Product:

public class Product
{
    public int ID              { get; set; }
    public string Manufacturer { get; set; }
    public string Model        { get; set; }
    public string PartNumber   { get; set; }
    public int CategoryID      { get; set; }
    public string Description  { get; set; }

    [NotMapped]
    public int VerticalID = 1;

    public virtual ProductCategory Category { get; set; }
    public virtual ProductPrice Price { get; set; }
    public virtual ICollection<ProductImage> Images { get; set; }
    public virtual ICollection<ProductDocument> Documents { get; set; }
    public virtual ICollection<ProductDetail> Details { get; set; }
    public virtual ICollection<RelatedProduct> RelatedProducts { get; set; }
}

现在,当实际上是试图执行此,我收到以下错误:

Now, when actually trying to execute this, I am getting the following error:

Unable to determine the principal end of an association between the types 'Print_Solutions.Models.ProductPrice' and 'Print_Solutions.Models.Product'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

如何判断实体同时使用VerticalID和Product.ID检索时的价格?(使用测试数据我有,如果这是产品1,本品应映射到的ID 1产品价格表,花费$ 100)。

How can I tell entity to use both VerticalID and Product.ID when retrieving the price? (using the test data I have, if this was product 1, this product should map to ID 1 of the product price table, and cost $100).

推荐答案

假设你的的DbContext 的集合 productSku,则名为 ProductPrices ,使用LINQ您只需必须做出这个查询:

Supposing your DbContext has a collection of ProductPrice named ProductPrices, using LINQ you simply has to make this query:

var price = ctx.ProductPrices.Where(pp =>
   pp.ProductId = productId && pp.VerticalId == verticalId).SingleOrDefault();

其中,的productId verticalId 是来自行动paramters,会话,或任何可用的paramters他们是。

Where productId and verticalId are the available paramters that come from the action paramters, the session, or wherever they are.

使用单或默认的担保,有只有一个值的数据库,或者说是没有的,而且,在这种情况下,你会得到结果的查询。

The use of single or default warranties that there's only one value on the database, or that there is none, and, on that case, you get null as a result of the query.

至于您的更新我看到你的问题也涉及到模型中的关系的定义。

As for your updates I see that your problem is also related to the definition of the relations in the model.

有3种方法来实现它:


  • 使用EF约定。为了更好地实现这一点,改变你的entites的ID属性的名称:例如使用产品编号,而不是 ID 和公约将建立模型对您

  • 使用属性。在这种特殊情况下使用 ForeignKeyAttribute 它适用


  • using EF conventions. To achive this, change the name of the ID properties of your entites: for example use ProductId, instead of ID and the conventions will build the model for you
  • using attributes. In this particular case use ForeignKeyAttribute where it applies
  • using the fluent API

您有关系的一些更多的信息,有几个简单的样本。

You have some more info on relationships here, with a few simple samples.

这篇关于我如何获得的EntityFramework检查2参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:18