0

I have started using Entity Framework Code First modeling technique and have seen many examples of implementing one to many (1-N) relationship using DataAnnotation and FluentAPI but all examples are using ICollection while modeling domain classes. I have already used generic ObservableCollection in my domain classes and do not intent to change it.

Currently while specifying the configuration using FluentAPI, i am getting following error:

HasRequired(t => t.App)
    .WithMany(t => t.EndPoints) // error here
    .HasForeignKey(t => t.App);

Cannot implicitly convert Type 'EndPoints' to 'ICollection'.

Note: EndPoints class is implemented using ObservableCollection.

My question is how to make it work?

Following is my entity definition:

public class ModelBase 
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class RuleApp : ModelBase 
{         
    public EndPoints EndPoints { get; set; }
}

public class EndPoint : ModelBase
{
    public RuleApp RuleApp { get; set; }
}

public class EndPoints : GenericObservableCollection<EndPoint> { }

public class GenericObservableCollection<T> : ObservableCollection<T> 
{ 
    // other common stuff handling
}
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93

1 Answers1

0

This is an Example to do that :

public class ModelBase 
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class RuleApp : ModelBase 
{       
//This for create RelationShip
    public virtual ICollection<EndPoint> EndPoints { get; set; }

    [NotMapped]
    Public EndPoints GenericEndPoints { get; set; }

    public void TransferToGenric()
    {
            GenericEndPoints =new EndPoints(EndPoints)
    }

}

public class EndPoint : ModelBase
{
    public RuleApp RuleApp { get; set; }
}

public class EndPoints : GenericObservableCollection<EndPoint> { }

public class GenericObservableCollection<T> : ObservableCollection<T> 
{ 
    // other common stuff handling
}

If you use GenericObservableCollection as a Property EF Mapped all property in your calss, so I create just a property to use endpoint and after that i transform it to the GenericObserveableCollection. in the constractor of you EndPoins class you have to featch all data in endpoint to do what you want

Mahdi Farhani
  • 964
  • 1
  • 9
  • 22
  • I know about this approach but this is not the convenient way to do it. I am looking a way which will not to affect my domain classes. – Furqan Safdar Jul 27 '16 at 09:18
  • why you dont want to change your domain ? – Mahdi Farhani Jul 27 '16 at 10:32
  • Primarily because my model/domain class are binding to enterprise WPF application, changing them for EF is never a good idea. And i think there must be some solution to my problem otherwise i might have to go for any other O/R mapper. – Furqan Safdar Jul 27 '16 at 13:48
  • Actually you don't need to change your model/domain class, just you have to map it correctly.for example I change endpoints property to GenericEndPoints, so don't change it and just ignore it to mapped. and create new property to make your relation.one of the benefit of using EF or some ORM,it's working with legacy system, like your application – Mahdi Farhani Jul 27 '16 at 18:57