Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

c# - How can I set up two navigation properties of the same type in Entity Framework

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; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...