如何设置了相同类型的实体框架的两个导航性能 [英] How can I set up two navigation properties of the same type in Entity Framework

查看:149
本文介绍了如何设置了相同类型的实体框架的两个导航性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用code首先EF4(使用CTP5)我可以与外键添加一个导航属性​​,它会尊重命名和唯一的外键添加到表一次。如果我再去看添加相同类型的第二个属性,它分解成4列在表上,而不是只有两个。

样品code:

通过这种模式,我得到一个属性添加到AdapterFrameCapability表命名为pressTypeID pressType。

 公共类AdapterFrameCapability
{
    [键]
    公众诠释AdapterFrameCapabilityID {获得;组; }

    [需要]
    公众诠释pressTypeID {获得;组; }

    公共虚拟pressType pressType {获得;组; }
}
 

这是我想要的模型设定,但它导致4列在表中被创建,分别从pressTypeID,起价pressTypeFrom pressTypeID,以pressTypeID和要pressType pressTypeID。理想情况下我只是想从pressTypeID和为pressTypeID列。我在做什么错在这里?

 公共类AdapterFrameCapability
{
    [键]
    公众诠释AdapterFrameCapabilityID {获得;组; }

    [需要]
    公众诠释从pressTypeID {获得;组; }

    [显示(名称=从preSS型)
    公共虚拟pressType从pressType {获得;组; }

    [需要]
    公众诠释为pressTypeID {获得;组; }

    [显示(名称为为preSS型)
    公共虚拟pressType为pressType {获得;组; }
}
 

解决方案

这是对那些需要下拉情形之一<一href="http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx">fluent API ,以获得所需的架构:

 保护覆盖无效OnModelCreating(模型构建器模型构建器)
{
    modelBuilder.Entity&LT; AdapterFrameCapability&GT;()
                .HasRequired(AFC =&GT; afc.From pressType)
                .WithMany()
                .HasForeignKey(AFC =&GT; afc.From pressTypeID)
                .WillCascadeOnDelete(真正的);

    modelBuilder.Entity&LT; AdapterFrameCapability&GT;()
                .HasRequired(AFC =&GT; afc.To pressType)
                .WithMany()
                .HasForeignKey(AFC =&GT; afc.To pressTypeID)
                .WillCascadeOnDelete(假);
}
 

切换级联的协会之一删除掉是故意的,因为,否则的SQL服务器会抛出以下错误:

  

在表'AdapterFrameCapabilities引进国外KEY约束'AdapterFrameCapability_To pressType'可能会导致循环或多重级联路径。指定ON DELETE NO行动或UPDATE NO ACTION或修改另一个外键约束。   无法创建约束。   

因此​​,我们需要关闭就像我在code做了这样的协会之一打开它。

With code first EF4 (using CTP5) I can add a single navigation property along with the foreign key and it will respect the naming and only add the foreign key to the table a single time. If I then go and add a second property of the same type, it breaks it down into 4 columns on the table instead of just two.

Sample code:

With this model, I get a single property added to the AdapterFrameCapability table for PressType named PressTypeID.

public class AdapterFrameCapability
{
    [Key]
    public int AdapterFrameCapabilityID { get; set; }

    [Required]
    public int PressTypeID { get; set; }

    public virtual PressType PressType { get; set; }
}

This is the setup I want to model, but it results in 4 columns being created in the table, one each for FromPressTypeID, FromPressTypeFromPressTypeID, ToPressTypeID and ToPressTypePressTypeID. Ideally I'd just like a column for FromPressTypeID and ToPressTypeID. What am I doing wrong here?

public class AdapterFrameCapability
{
    [Key]
    public int AdapterFrameCapabilityID { get; set; }

    [Required]
    public int FromPressTypeID { get; set; }

    [Display(Name = "From Press Type")]
    public virtual PressType FromPressType { get; set; }

    [Required]
    public int ToPressTypeID { get; set; }

    [Display(Name = "To Press Type")]
    public virtual PressType ToPressType { get; set; }
}

解决方案

It's one of those scenarios that you need to drop down fluent API to get the desired schema:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<AdapterFrameCapability>()
                .HasRequired(afc => afc.FromPressType)
                .WithMany()
                .HasForeignKey(afc => afc.FromPressTypeID)
                .WillCascadeOnDelete(true);

    modelBuilder.Entity<AdapterFrameCapability>()
                .HasRequired(afc => afc.ToPressType)
                .WithMany()
                .HasForeignKey(afc => afc.ToPressTypeID)
                .WillCascadeOnDelete(false);
}

Switching cascade delete off on one of the associations is intentional because otherwise SQL Server would throw out the following error:

Introducing FOREIGN KEY constraint 'AdapterFrameCapability_ToPressType' on table 'AdapterFrameCapabilities' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint.

So we need to switch it off on one of the associations like the way I did in the code.

这篇关于如何设置了相同类型的实体框架的两个导航性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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