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; }
}
Navigation properties provide a way to navigate an association between two entity types. Every object can have a navigation property for every relationship in which it participates.
An Entity can include two types of properties: Scalar Properties and Navigation Properties. Scalar Property: The type of primitive property is called scalar properties. Each scalar property maps to a column in the database table which stores the real data.
Entity framework supports three types of relationships, same as database: 1) One-to-One 2) One-to-Many, and 3) Many-to-Many. We have created an Entity Data Model for the SchoolDB database in the Create Entity Data Model chapter.
Navigation properties allow a user to navigate from one entity to another, or from one entity to related entities through an association set. This topic provides examples in query expression syntax of how to navigate relationships through navigation properties in LINQ to Entities queries.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With