How to create nullable one-to-many relationship in Entity Framwork 6 code first

c# entity-framework entity-framework-6

Question

I'm using EF 6.1 code to create an application. The following classes are included in the entity model:

public enum UnitSystem {
    English,
    Metric
}

public enum UnitTypes {
    Volume,
    Weight,
    Other
}

public class Unit {

    [DatabaseGenerated( DatabaseGeneratedOption.Identity ), Key]
    public int UnitId { get; set; }

    [Required, MaxLength( 128 ), Index( IsUnique = true )]
    public string UnitName { get; set; }

    [Required]
    public UnitSystem UnitSystem { get; set; }

    [Required]
    public UnitTypes UnitType { get; set; }

    public virtual ICollection<Item> Items { get; set; }
}

public class Item {

    [DatabaseGenerated( DatabaseGeneratedOption.Identity ), Key]
    public int ItemId { get; set; }

    [Required, MaxLength( 128 ), Index( IsUnique = true )]
    public string ItemName { get; set; 

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

    [ForeignKey( "UnitId" )]
    public virtual Unit Units { get; set; }

    /// Other unimportant properties
}

public class UnitConversion {

    [Required, Key, Column( Order = 0 )]
    public int FromUnitId { get; set; }

    [ForeignKey( "FromUnitId" )]
    public virtual Unit FromUnit { get; set; }

    [Required, Key, Column( Order = 1)]
    public int ToUnitId { get; set; }

    [ForeignKey( "ToUnitId" )]
    public virtual Unit ToUnit { get; set; }

    [Key, Column( Order = 2 )]
    public int? ItemId { get; set; }

    public virtual Item Item { get; set; }

    [Required]
    public double ConversionFactor { get; set; }
}

I get the "Introducing FOREIGN KEY CONSTRAINT" notification when EF creates the database. In order toItemId property must be capable of being null, and if it isn't null, it must be a foreign key into theItems table.

How should this relationship be established correctly to prevent the cascading deletes error?

Edit

I'm no longer experiencing the issue with theItemId I can now use it for either the columnFromUnitId or ToUnitId or both columns.

Navigation properties have been added to theUnit class:

[ForeignKey("FromUnitId")]
public virtual ICollection<UnitConversion> FromConversions { get; set; }

[ForeignKey("ToUnitId")]
public virtual ICollection<UnitConversion> ToConversions { get; set; }

And the complete content of the error notice I receive is as follows:

Introducing FOREIGN KEY constraint 'FK_dbo.UnitConversions_dbo.Units_ToUnitId' on table 'UnitConversions' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

A zero-or-one to many relationship is meant to exist between theUnitConversions the table andUnits table. I'm attempting to create a call tomodelBuiloder.Entity<UnitConversions>().WillCascadeOnDelete(false) when using IntelliSense, theWillCascadeOnDelete method, the code will not build.

1
-1
5/6/2016 3:20:24 PM

Accepted Answer

ZZZ_tmp
0
5/5/2016 8:08:33 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow