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

associations c# entity-framework mapping one-to-many

Question

I've got anAsp_Users a table andResource tables. I have a question related to Entity Framework.Asp_Users POC and anResource POCO. Furthermore, theResource As a component of the Table-per-Hierarchy paradigm, POCO is abstract. The abstract in the Table-per-Hierarchy modelResource POCO and manyConcrete POCOs enjoyILCResource APO andSectionResource POCO. A one-to-many (1 to 0*) relationship exists betweenAsp_Users ONE-SIDED POCO TOResource POCO (many-side).

The pertinent portion of my aspnet Users POCO is as follows:

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 Asp Users mapping settings.

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);
    }
} 

The pertinent portion of my abstract Resource POCO class is as follows:

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 resource mapping configuration:

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);
    }
}

Please let me know why I'm seeing the following error.

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

Please elucidate the reason why it remains NULL after my creation of the mapping in the POCO class.

enter image description here

1
3
6/21/2015 9:39:16 AM

Accepted Answer

I must learn more about theIQueryable and/or IEnumerable placing them in the aforementioned collections and the lambda expression queries that are used to get dataIQueryable and/or IEnumerable .

My issue was that I was attempting to extract a specific POCO's relationships within an iterative loop that cycles acrossIQueryable and/or IEnumerable collections.

Here is the flawed code (when iterating, we are accessing associations).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 correct code that should fix the problem:

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);
    }
}
0
6/21/2015 9:46:05 AM

Popular Answer

ZZZ_tmp


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