none
Multiplicity constraint violated, The role X of the relationship Y has multiplicity 1 or 0..1. RRS feed

  • Question

  • Using code first (EF 6), I created a 1 parent - 2 child relationship. Here is the object image below. Property is the parent object and Property Address as a child with 1 or 0..1 relationship. PropertyImage is another child with 1 to many relationship. This one works fine but the PropertyAddress throws error if I try to eager load (Test Function).

    Actual Error -

    Multiplicity constraint violated.
    The role 'PropertyAddress_Property_Source' of the relationship 'MyAssetTracker.DataLayer.Models.PropertyAddress_Property' has multiplicity 1 or 0..1.

    // Test Function GetProperty() { Property property; using (var repo = new PropertyRepository()) { property = repo.AllIncluding(a=>a.Images, a=>a.Address).FirstOrDefault(a => a.Id == testpropertyid); } } //Property Repository public class PropertyRepository : IPropertyRepository {

    public IQueryable<Property> AllIncluding(params Expression<Func<Property, object>>[] includeProperties) { IQueryable<Property> query = context.Properties; foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } return query; } }


     

     

    Wednesday, January 8, 2014 4:31 PM

Answers

  • Hi Andy-T1,

    same issue here - in similar scenario - my datas lazy loaded.

    Storing data works fine, reading causes this error.

    Assert.IsNotNull on the entity dont fire ... BUT the ChangeTracker says: HasChanges after that read...  

    First attempts without Back-Navigation-Properties dont throw an error - but - reading results with null.

    Only workaround for me was to set the relations to HasMany on the back-navigation-side

    Saturday, January 11, 2014 7:27 PM
  • Hello Andy-T1,

    >>This one works fine but the PropertyAddress throws error if I try to eager load (Test Function).

    It is very strange, because I create the similar table schema like below:

    I did a query with both eager load and lazy load and both worked fine, however. My test function is like below:

    internal void Execute()
    
            {
    
                using (Model1Container db = new Model1Container())
    
                {
    
                    db.Configuration.LazyLoadingEnabled = false;
    
    
                    var property = AllIncluding(a => a.Images, a => a.Address, db).FirstOrDefault(p => p.PropertyID == 1);
    
                }
    
            }
    
    
            private IQueryable<Property> AllIncluding(Expression<Func<Property, object>> func1, Expression<Func<Property, object>> func2, Model1Container db)
    
            {
    
                IQueryable<Property> query = db.Properties;
    
    
                query = query.Include(func1);
    
    
                query = query.Include(func2);
    
    
                return query;
    
            }
    

    I notice that your actual table schema is more complex, could you please share it with us so that we can know it more clearly. And please have a try to create new table schema with only the three tables to check whether it will throw the error again.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 10, 2014 8:43 AM
    Moderator

All replies

  • Hello Andy-T1,

    >>This one works fine but the PropertyAddress throws error if I try to eager load (Test Function).

    It is very strange, because I create the similar table schema like below:

    I did a query with both eager load and lazy load and both worked fine, however. My test function is like below:

    internal void Execute()
    
            {
    
                using (Model1Container db = new Model1Container())
    
                {
    
                    db.Configuration.LazyLoadingEnabled = false;
    
    
                    var property = AllIncluding(a => a.Images, a => a.Address, db).FirstOrDefault(p => p.PropertyID == 1);
    
                }
    
            }
    
    
            private IQueryable<Property> AllIncluding(Expression<Func<Property, object>> func1, Expression<Func<Property, object>> func2, Model1Container db)
    
            {
    
                IQueryable<Property> query = db.Properties;
    
    
                query = query.Include(func1);
    
    
                query = query.Include(func2);
    
    
                return query;
    
            }
    

    I notice that your actual table schema is more complex, could you please share it with us so that we can know it more clearly. And please have a try to create new table schema with only the three tables to check whether it will throw the error again.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 10, 2014 8:43 AM
    Moderator
  • Hi Andy-T1,

    same issue here - in similar scenario - my datas lazy loaded.

    Storing data works fine, reading causes this error.

    Assert.IsNotNull on the entity dont fire ... BUT the ChangeTracker says: HasChanges after that read...  

    First attempts without Back-Navigation-Properties dont throw an error - but - reading results with null.

    Only workaround for me was to set the relations to HasMany on the back-navigation-side

    Saturday, January 11, 2014 7:27 PM