Why is The navigation property '' declared on type '' has been configured with conflicting multiplicities. error show up?

10,575

Solution 1

I need to study more about the IQueryable and/or IEnumerable collections along with the lambda expression queries that are used to retrieve data, and place them in the aforementioned IQueryable and/or IEnumerable.

My problem was that I was trying to retrieve the associations of a particular POCO within a loop that iterates over the IQueryable and/or IEnumerable collections.

Here is the Faulty code (we are accessing association while we iterate over IQueryable):

resourcestempIQueryable = context.Resources;
foreach (PerlsData.Domain.Resource tempRes in resourcestempIQueryable)
{
    string[] resourceRow = { tempRes.ResourceName,
                             tempRes.DescriptionOfResource,
                             tempRes.UploadDate.ToString(),
                             tempRes.Iconpath,
                             tempRes.aspnet_Users.UserName, // Error
                             tempRes.ResourceDatabaseID.ToString() };
    tempDataTableForResources.Rows.Add(resourceRow);
}

Here is the proper code that should resolve the issue:

using (PerlsData.Context context = new PerlsData.Context())
{
    context.Configuration.LazyLoadingEnabled = true;
    context.Configuration.ProxyCreationEnabled = true;
    resourcesIEnumerable = context.Resources.ToList<PerlsData.Domain.Resource>();

    var entityValuesWithFieldsToShowInAllResourcesPage = context.Resources.Select(res => new { res.ResourceName, res.DescriptionOfResource, res.UploadDate, res.Iconpath, res.aspnet_Users.UserName, res.ResourceDatabaseID });

    foreach (var varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage in entityValuesWithFieldsToShowInAllResourcesPage)
    {
        string[] resourceRow = { 
            varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.ResourceName, 
            varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.DescriptionOfResource, 
            varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.UploadDate.ToString(), 
            varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.Iconpath, 
            varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.UserName, 
            varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.ResourceDatabaseID.ToString()
        };
        tempDataTableForResources.Rows.Add(resourceRow);
    }
}

Solution 2

this.HasOptional(u => u.associatedResources);

That's wrong. HasOptional means 0..1.
You want HasMany().

In fact, you can get rid of that line entirely; EF can figure it out from the property.

Share:
10,575
crazyTech
Author by

crazyTech

John 3:16 For God so loved the world, that He gave His only begotten Son, that whoever believes in Him shall not perish, but have eternal life.

Updated on June 04, 2022

Comments

  • crazyTech
    crazyTech almost 2 years

    I have an Asp_Users table and a Resource tables. In the Entity Framework world, I have a Asp_Users POCO and a Resource POCO. Moreover, the Resource POCO is abstract and is part of a Table-per-Hierarchy model. The Table-per-Hierarchy model has the abstract Resource POCO and several Concrete POCOs like ILCResource POCO and SectionResource POCO. There is a one-to-many (1 to 0…*) relationship from Asp_Users POCO (one-side) to Resource POCO (many-side).

    Here's the relevant part of my aspnet_Users POCO:

    public partial class aspnet_Users
    {
        public aspnet_Users() { }
    
        public virtual System.Guid ApplicationId { get; set; }
        public virtual System.Guid UserId { get; set; }
        public virtual string UserName { get; set; }
        public virtual string LoweredUserName { get; set; }
        public virtual string MobileAlias { get; set; }
        public virtual bool IsAnonymous { get; set; }
        public virtual System.DateTime LastActivityDate { get; set; }
    
        public virtual ICollection<Resource> associatedResources { get; set; }
    }
    

    Here is my mapping configuration for Asp_Users

    public class Aspnet_UsersMap : EntityTypeConfiguration<PerlsData.Domain.aspnet_Users>
    {
        public Aspnet_UsersMap()
        {
            this.ToTable("aspnet_Users", schemaName: "dbo");
    
            this.HasKey(u => u.UserId);
    
            this.Property(u => u.UserId)
                .HasColumnName("UserId")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.HasOptional(u => u.associatedResources);
        }
    } 
    

    Here's the relevant part of my abstract Resource POCO class:

    public abstract class Resource
    {   
        public Resource(){
           // associatedPerspectives = new HashSet<Perspective>();
        }
    
        public virtual System.Guid ResourceDatabaseID { get; set; }
        public virtual string ResourceName { get; set; }
        public virtual string DescriptionOfResource { get; set; }
        public virtual System.Guid UserId { get; set; }
        public virtual Nullable<System.Guid> DepartmentDatabaseID { get; set; }
        public virtual string ResourceStatus { get; set; }
        public virtual Nullable<short> isRemoved { get; set; }
    
        public virtual Department Department { get; set; }
    
        public virtual System.Guid UserId { get; set; }
        public virtual aspnet_Users aspnet_Users { get; set; }
    
    
        public virtual ICollection<ResourceOverToILCResourcesBridge> associatedResourceOverToILCResourcesBridgeEntry { get; set; }
    }
    

    Here is my mapping configuration for Resource:

    public class ResourceMap : EntityTypeConfiguration<Resource>
    {
    
        public ResourceMap()
        {
            this.ToTable("Resources", schemaName: "dbo");
    
            this.Property(r => r.ResourceDatabaseID)
                .HasColumnName("ResourceDatabaseID");
    
            this.HasKey(r => r.ResourceDatabaseID);
    
            this.Property(x => x.ResourceDatabaseID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            // .StoreGeneratedPattern = StoreGeneratedPattern.Identity;
            this.Property(r => r.ResourceName)
                .HasColumnName("ResourceName");
    
            this.Map<PerlsData.Domain.OtherItem>(m => m.Requires("discriminator").HasValue("otheritems"))
            .Map<PerlsData.Domain.Audioitem>(m => m.Requires("discriminator").HasValue("audioitems"))
            .Map<PerlsData.Domain.Imageitem>(m => m.Requires("discriminator").HasValue("imageitems"))
            .Map<PerlsData.Domain.Videoitem>(m => m.Requires("discriminator").HasValue("videoitems"))
            .Map<PerlsData.Domain.UriItem>(m => m.Requires("discriminator").HasValue("uriitems"))
            .Map<PerlsData.Domain.Documentitem>(m => m.Requires("discriminator").HasValue("documentitems"))
            .Map<PerlsData.Domain.DatabaseFileItem>(m => m.Requires("discriminator").HasValue("databasefileitems"));
    
            this.HasOptional(res => res.associatedResourceOverToILCResourcesBridgeEntry);
    
            this.HasRequired(res => res.aspnet_Users)
                .WithMany(u => u.associatedResources)
                .HasForeignKey(res => res.UserId);
        }
    }
    

    Could you please tell me why I am getting the following error?

    The navigation property 'associatedResources' declared on type 'PerlsData.Domain.aspnet_Users' has been configured with conflicting multiplicities.

    Please Explain Why it's still NULL after I created the mapping in the POCO class.

    enter image description here