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

asp.net code-first entity-framework relationship

Question

Having some issues with my relationships. A Campaign has pages, a page has a CssFile. When I query all the Campaigns from the database the error (see below) gets thrown.

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

For one-to-one relationships, EF expects that the tables are using the same primary key. And really, if it's a true one-to-one they probably should. So in your example, if you make UserID the primary key on the UserProfiles table, your one-to-one will work.

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

Popular Answer

I have a similar issue, but with a sale and layby scenario.

A layby can exist without a sale, and a sale can exist without a layby. This means I have a 0 or 1 to 0 or 1 relationship.

Layby references sale, but layby cannot use the primary key of Sale, and Sale cannot use the primary key of Layby.

I solved the problem by using a 0 or 1 to many relationship, configured the 'Laybys' getter and setter on the sale as private, and then provided 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