First, the ASP.NET EF code: During model creation, one or more validation problems were discovered.

asp.net code-first entity-framework relationship

Question

I'm having some relationship problems. A page has a CssFile, and a campaign has pages. The issue (see below) is thrown when I do a database query on all of the campaigns.

public IQueryable<Campaign> FindAll()
{
    return campaigns.Include(c => c.Pages).AsQueryable();
}

 public class Page
    {
        [Key()]
        public int Id { get; set; }
        public string Logo { get; set; }
        public string LogoAlt { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Footer { get; set; }
        [ForeignKey("CssFile")]
        public int CssFileId { get; set; }
        public virtual CssFile CssFile { get; set; }
        [ForeignKey("Campaign")]
        public int CampaignId { get; set; }
        public virtual Campaign Campaign { get; set; }

        public Page()
        {
           CssFile = new CssFile(this, "default.css");
           CssFileId = CssFile.CssFileId;
        }
        public override bool Equals(object obj)
        {
            if (!(obj is Page))
                return false;
            return ((Page)obj).Id == this.Id;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }



public class CssFile
    {
        public const string PATH = "../../Uploads/";
        [Key()]
        public int CssFileId { get; set; }
        public string FileName { get; set; }
        public string Content { get; set; }

        [ForeignKey("Page")]
        public int PageId { get; set; }
        public virtual Page Page { get; set; }

        public CssFile()
        {
            FileName = "default.css";
            Content = "";
        }
        public CssFile(Page page, string name, string content)
        {
            Page = page;
            PageId = page.Id;
            this.FileName = name;
            this.Content = content;
        }
        public CssFile(Page page, string name)
        {
            Page = page;
            PageId = page.Id;
            this.FileName = name;
            Content = "";
        }
}

Error:

One or more validation errors were detected during model generation:

System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Page_CssFile_Target' in relationship 'Page_CssFile'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be �*�.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Page_CssFile_Target' in relationship 'Page_CssFile'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be �*�.

1
0
4/11/2012 2:44:28 PM

Accepted Answer

EF anticipates that the tables will be utilizing the same primary key for one-to-one connections. And if it's a genuine one-to-one conversation, they most likely should. Therefore, in your example, the one-to-one relationship will function if UserID is set as the primary key on the UserProfiles database.

66
11/19/2009 7:00:37 AM

Popular Answer

In my case, the problem involves a sale and layaway arrangement.

Both a layby and a sale may happen independently of one another. This signifies that my relationship ranges from zzz-9 to zzz-13.

Layby makes reference to Sale, but neither Layby nor Sale may utilize the main key of the other.

I used a 0 or 1 to many relationship to address the issue, changed the sale's "Laybys" getter and setter to private, and then included my own "Layby" getter and setter in my POCO.



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