多重性和EF 6的问题

时间:2014-02-25 19:22:03

标签: c# entity-framework

这发生在没有地方。我之前从未遇到过这个问题。我刚刚在我的SQL Azure数据库中添加了一个表,该表将为注册我们的电子邮件列表的人保留电子邮件。没有关联到该表的关联,它只是单独的。我回到VS并从我的数据库更新我的模型,现在得到这些错误。

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

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

据我所知,它正在谈论关系中的一个问题,但我没有改变任何事情,也不明白为什么现在抱怨。

我查了一下http://msdn.microsoft.com/en-us/data/jj591620.aspx 并最终改变了设计师与0..1 (Zero or One of Manufacturer)之间的关系 到1 (One of Manufacturer)最终解决了第一个错误。我对第二个错误做了同样的事情,现在问题都消失了。我不完全确定这里发生了什么,我担心这样继续我的项目可能会导致更多的问题。

任何人都可以提供任何有关可能出错的信息吗?

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 个答案:

答案 0 :(得分:11)

你需要一个1->许多关系如果你想拥有不可为空的密钥。将所讨论的关系更改为0 - &gt;许多。

我假设关系约束在某些时候发生了变化,这是一个数据第一个项目吗?如果是,您确定在数据库级别没有更改约束吗?