EF 4 CTP 5多个条件映射 [英] EF 4 CTP 5 many-many conditional mapping

查看:92
本文介绍了EF 4 CTP 5多个条件映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Code First流畅的API在许多场景中进行某种条件映射?

Is it possible to have some kind of conditional mapping in many-many scenarios using the Code First fluent API?

假设我们有一个产品表和2个表simplecategory和specialcategory。

Let's say, we have a product table and 2 tables simplecategory and specialcategory.

两者共享一个映射表productcategories。

Both share one mapping table productcategories.

这是simplecategory关系的流畅配置:

This is the fluent configuration for the simplecategory relation:

  modelBuilder.Entity< Product>()

               &NBSP;&NBSP; .HasMany(p => p.SimpleCategories)               


                 .WithMany(C => c.Products)&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;


                 .Map(

                 mc =>

                 {

                      mc.ToTable(" ProductCategories");

                     mc.MapLeftKey(p = > p.Id,""ProductID");

                      mc.MapRightKey(c => c.Id," CategoryID");                    


            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
                 );

 modelBuilder.Entity<Product>()
                 .HasMany(p => p.SimpleCategories)               
                 .WithMany(c=>c.Products)               
                 .Map(
                 mc =>
                 {
                     mc.ToTable("ProductCategories");
                     mc.MapLeftKey(p => p.Id, "ProductID");
                     mc.MapRightKey(c => c.Id, "CategoryID");                   
                 }
                 );

对于某些条件,映射表中的值,或者甚至是simplecategory表中更好的值,

我想改为使用specialcategory映射,或以某种方式从产品的类别集合中注入值。

For certain conditions, either values in the mapping table, or even better values in the simplecategory table,
I would like to take the specialcategory mapping instead, or somehow inject the values from there in that categories collection of the product.

 

推荐答案

Code First(或一般的EF)不支持此功能。我的建议是在你的数据库中创建几个视图,以便从连接表中拆分记录,然后使用Code First映射两个单独的关系(映射到视图而不是基础表的
)。

This isn't supported in Code First (or EF in general). My suggestion would be to create a couple of views in your database to split up the records from the join table and then map two separate relationships using Code First (mapping to the views rather than the underlying table).

~Rowan


这篇关于EF 4 CTP 5多个条件映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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