Problems with multiplicity and EF 6

c# entity-framework

Question

This happened out of no where. I never had this issue before. I just finished adding a table to my SQL Azure database which will hold emails for people who sign up for our email list. There are no associations tied to that table, it is simply alone. I go back to VS and update my model from my database and now get these errors.

Error   1   Error 113: Multiplicity conflicts with the referential constraint in Role 'Category' in relationship 'FK_Items_3'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.    C:\Users\chuyita\Documents\Visual Studio 2012\Projects\gcaMusicExchange\gcaMusicExchange\myConnection.edmx  1043    11  gcaMusicExchange

and

Error   2   Error 113: Multiplicity conflicts with the referential constraint in Role 'Manufacturer' in relationship 'FK_Items_4'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.    C:\Users\chuyita\Documents\Visual Studio 2012\Projects\gcaMusicExchange\gcaMusicExchange\myConnection.edmx  1055    11  gcaMusicExchange

I understand it is talking about an issue with the relationships but I have not changed anything and don't understand why it is now complaining.

I checked this out http://msdn.microsoft.com/en-us/data/jj591620.aspx and ended up changing my 2 relationships in the designer from 0..1 (Zero or One of Manufacturer) to 1 (One of Manufacturer) which ended up solving the first error. I did the same with the second error and now the problems are gone. I am not completely sure what is going on here and am afraid continuing my project this way might lead to more problems down the road.

Can anyone offer any insight as to what may have gone wrong?

public partial class Category
{
    public Category()
    {
        this.Items = new HashSet<Item>();
    }

    public int id { get; set; }
    public string Name { get; set; }

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

 public partial class Manufacturer
{
    public Manufacturer()
    {
        this.Items = new HashSet<Item>();
    }

    public int id { get; set; }
    public string Name { get; set; }

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

public partial class Item
{
    public Item()
    {
        this.PrivateMessages = new HashSet<PrivateMessage>();
    }

    public int id { get; set; }
    public int SellingUserId { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public Nullable<decimal> Price { get; set; }
    public Nullable<bool> IsActive { get; set; }
    public Nullable<System.DateTime> PostDate { get; set; }
    public Nullable<System.DateTime> LastGarbageCollectionDate { get; set; }
    public Nullable<int> SoldToUserId { get; set; }
    public string Uri1 { get; set; }
    public string Uri2 { get; set; }
    public Nullable<int> ZipCode { get; set; }
    public int CategoryId { get; set; }
    public int ManufacturerId { get; set; }
    public Nullable<bool> WaitingForSoldConfirmation { get; set; }
    public Nullable<int> PendingSoldTo { get; set; }
    public Nullable<int> Views { get; set; }
    public Nullable<bool> AcceptsTrades { get; set; }
    public Nullable<int> MakeId { get; set; }
    public Nullable<int> ModelId { get; set; }
    public Nullable<int> YearId { get; set; }
    public Nullable<int> Type { get; set; }
    public string Uri3 { get; set; }
    public string Uri4 { get; set; }
    public string Uri5 { get; set; }

    public virtual Category Category { get; set; }
    public virtual Manufacturer Manufacturer { get; set; }
    public virtual VehicleMake VehicleMake { get; set; }
    public virtual VehicleModel VehicleModel { get; set; }
    public virtual VehicleYear VehicleYear { get; set; }
    public virtual ICollection<PrivateMessage> PrivateMessages { get; set; }
}
1
7
2/25/2014 7:22:03 PM

Accepted Answer

You need a 1-> many relationship if you want to have non-nullable keys. Change the relationship in question to be 0 -> many.

I am assuming the relationship constraint was changed at some point, is this a data first project? If so are you sure that the constraint wasn't changed at the database level?

11
2/25/2014 7:42:01 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