具有附加过滤器参数的复合键(代码优先EF4) [英] composite keys with additional filter parameter (code-first EF4)

查看:77
本文介绍了具有附加过滤器参数的复合键(代码优先EF4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个带有复合键的模型,在我的情况下,该模型具有多个版本的描述。


除了约束之外,我还需要填写说明.IsActiveRevision == 1 


运行时我得到一个例外,约束必须与键匹配......任何想法如何解决这个问题?


我的映射看起来像这样...... 


 modelBuilder.Entity< Product>()
.HasKey(p => new {p.ProductID,p.Brand})
.HasOptional( g => g.Description).WithMany(x => x.Descriptions)
.HasConstraint((product,description)=>
product.ProductID == description.ProductID&&
product.BrandID == description.BrandID&&
description.IsActiveRevision == 1);

解决方案

< blockquote>


 


HasConstraint的命名可能会引起混淆,HasConstraint实际上是HasForeignKey。它被称为HasConstraint,因为在EF外键中是
,称为参照约束。我们可能会更改名称在下一个使它更直观。


 


为了实现你的目标,你需要定义Product - >描述关系为one:many然后在查询中使用过滤器来获取当前的
描述。


 


〜罗文


Hi,

I have a Model with a composite key which has multiple versions of the child in my case the description.

Beside the Constraints I need also to fillter the description.IsActiveRevision == 1

On runtime I get an exception that the constraint must match the keys... Any idea how to solve this?

My mapping looks like this...

 modelBuilder.Entity<Product>()
        .HasKey(p => new { p.ProductID, p.Brand })
        .HasOptional(g => g.Description).WithMany(x => x.Descriptions)
            .HasConstraint((product, description) => 
              product.ProductID== description.ProductID && 
              product.BrandID == description.BrandID &&
              description.IsActiveRevision == 1);

解决方案

Hi,

 

The naming of HasConstraint is probably causing confusion here, HasConstraint is really HasForeignKey. It’s called HasConstraint because in EF foreign keys are referred to as Referential Constraints. We’ll likely change the name in the next to make it more intuitive.

 

To achieve what you are after you would need to define the Product -> Description relationship as one:many and then use a filter in a query to get back the current description.

 

~Rowan


这篇关于具有附加过滤器参数的复合键(代码优先EF4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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