Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplicity of the Principal Role must be 0..1

I'm developing a C# library with .NET Framework 4.6.2 and Entity Framework 6.1.3 Code First to use it in a SQL Server 2012 database.

I have these two classes:

public class Product
{
    public int ProductId { get; set; }

    // Omitted for brevity

    public virtual ICollection<ProductionOrder> ProductionOrders { get; set; }
}

public class ProductionOrder
{
    public int ProductionOrderId { get; set; }
    public int? ProductId { get; set; }

    // Omitted for brevety

    public virtual Product Product { get; set; }

    // Omitted for brevity
}

With these two configuration classes:

class ProductionOrderConfiguration : EntityTypeConfiguration<ProductionOrder>
{
    public ProductionOrderConfiguration()
    {
        HasKey(po => po.ProductionOrderId);

        Property(c => c.ProductionOrderId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        Property(po => po.ProductionOrderId)
            .IsRequired();

        Property(po => po.ProductId)
            .IsOptional();

        // Omitted for brevity
    }
}

class ProductConfiguration : EntityTypeConfiguration<Product>
{
    public ProductConfiguration()
    {
        HasKey(p => p.ProductId);

        Property(p => p.ProductId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        // Omitted for brevity

        HasMany(p => p.ProductionOrders)
            .WithRequired(po => po.Product)
            .HasForeignKey(p => p.ProductId);
    }
}

But, when I try to run it, I get the following message that I don't understand:

Product_ProductionOrders: : Multiplicity is not valid in Role 'Product_ProductionOrders_Source' in relationship 'Product_ProductionOrders'. Because all the properties in the Dependent Role are nullable, multiplicity of the Principal Role must be '0..1'.

The model I'm trying to represent is:

A production order can have zero or one product. And a product could be in one or n production orders.

I don't know how to set multiplicity of the Principal Role to '0..1'.

like image 941
VansFannel Avatar asked Apr 04 '17 10:04

VansFannel


1 Answers

Well, it's just a matter of different terminology used in fluent API. The mapping is:

multiplicity 1     => Required
multiplicity 0..1  => Optional

According to your model, you need to change

.WithRequired(po => po.Product)

to

.WithOptional(po => po.Product)
like image 197
Ivan Stoev Avatar answered Oct 31 '22 12:10

Ivan Stoev