emitmapper circular reference - circular-reference

emit mapper circular reference issues. I am trying to map AA to A. A has object of B, but B has object of A. This is circular reference issue. I am not sure how Emit mapper can handle this issue.
public class A
{
public A()
{
list = new List<B>();
}
List<B> list {get; set;}
}
public class B
{
public A object {get; set;}
}
public class AA
{
public AA()
{
list= new List<BB>();
}
public List<BB> list {get; set;}
}
public class BB
{
public AA object {get; set;}
}
objectified = new A();
ObjectMapperManager.DefaultInstance.Get Mapper<A, AA>().Map(objectified);
Need to map from A to AA. Now I got the stack overflow error. Any one know how to resolve this issue?

The circular reference classes are created by EF power tools. And the generated classes need to be updated to one way parent-reference only class. So the issue is not related to the Emitmapper, the issue is the class design itself.
public class A
{
public A()
{
list = new List<B>();
}
List<B> list {get; set;}
}
public class B
{
//remove the following child-parent relationship.
//public A object {get; set;}
}

Related

AutoMapper ProjectTo not returning navigation properties

ProjectTo works fine when entities have navigation properties and matching properties on corresponding Dtos. what am i missing ?
in my case this does not work although the query has all data :
public class Category {
....
some properties
...
public Category ParentCategory {get; set;}
}
here is the Dto
public class CategoryDto : IMapFrom<Category>
{
public void Mapping(Profile profile)
{
profile.CreateMap().ReverseMap();
}
....
some properties
...
public CategoryDto ParentCategory {get; set;}
}

Entity Framework 5 - Many to Many relationship between multiple table using same intermediate table

I am trying to implement the below scenario using Entity Framework 5, with code first approach.
Scenario: I have three tables say
Table1: { Id1, Title }
Table2: { Id2, Title }
Table3: { Id3, Title }
and single intermediate table storing many-to-many relationship between "Table1 &Table2" and "Table2 & Table3". Say,
TableIntermediate: { FK1, FK2 }
To specify the relation, On OnModelCreating(), I have specified two model builder:
modelBuilder.Entity<Table1>()
.HasMany(c => c.Table2s)
.WithMany(pc => pc.Table1s)
.Map(m =>
{
m.ToTable("TableIntermediate");
m.MapLeftKey("FK1");
m.MapRightKey("FK2");
});
modelBuilder.Entity<Table1>()
.HasMany(c => c.Table3s)
.WithMany(pc => pc.Table1s)
.Map(m =>
{
m.ToTable("TableIntermediate");
m.MapLeftKey("FK1");
m.MapRightKey("FK2");
});
Below are the entities defined:
public class Table1
{
public int Id1 {get; set;}
public string Title {get; set;}
public ICollection<Table2> Table2s { get; set; }
public ICollection<Table3> Table3s { get; set; }
}
public class Table2
{
public int Id2 {get; set;}
public string Title {get; set;}
public ICollection<Table1> Table1s { get; set; }
}
public class Table3
{
public int Id3 {get; set;}
public string Title {get; set;}
public ICollection<Table1> Table1s { get; set; }
}
The code compiles properly. But at runtime throwing the error:
{"Schema specified is not valid. Errors: \r\n(275,6) : error 0019: The EntitySet 'Table1Table2' with schema 'XXXX' and table 'TableIntermediate' was already defined. Each EntitySet must refer to a unique schema and table."}
As we are using the exiting database, we want to store both the relations into a single intermediate table. The only solution I found is to introduce a new mapping table. Please let me know if the scenario is possible without introducing the new mapping table.

how to desactivate lazy loading for entity framework 6

public class Country : Item
{
public string Code
{get; set;}
public string CodePhone
{ get; set; }
public string Name
{get; set;}
public string Flag
{ get; set; }
public decimal? Latitude
{get; set;}
public decimal? Longitude
{get; set;}
public int RegionsCount
{get; set;}
[ForeignKey("DefaultCurrency")]
public int? DefaultCurrencyID
{get; set;}
public virtual Currency DefaultCurrency
{ get; set; }
public ContinentType ContinentType
{ get; set; }
public virtual ICollection<Property>
Properties
{get; set;}
public ICollection<CountryLocale>
CountryLocales
{ get; set; }
}
public class CountryLocale : ItemLocale
{
[ForeignKey("Country")]
public int CountryID
{get; set;}
public Country
Country
{get; set;}
public string FullName
{get;set;}
}
public TEntity Get(Expression<Func<TEntity, bool>> where, params string[] includes)
{
var model = this.DbSet;
foreach (var property in includes)
{
model.AsExpandable().Include(property);
}
return model.Where(where).FirstOrDefault();
}
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
Country country = this._CountryRepository.Get(p=>p.ID == this.CountryID, new string[] { "CountryLocales" });
Value cannot be null.
Parameter name: source
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.ArgumentNullException: Value cannot be null.
Parameter name: source
line : CountryLocale countrylo = country.CountryLocales.First();
i got this error when trying to desactivate lazyloading for performance reasons. how can i resolve the problem? thanks in advance.
As you disabled LazyLoading you need to manually load the child properties of your main object with Include
var country = db.Country.Include("CountryLocales");
The other way around would be:
Dont disable lazy load and use include in your queries.
Try including your child objects one by one until you feel the
performance is better.
Usually if you know you will iterate over a
child object you should use include in your query for that child
object.
remeber to include second level child if you need to access a property of that level... Include("Parent.Child")

automaticly serialize object via web service

I have my webservice method which sends an object as a result, for example:
public Dog GetDog();
where dog looks like this:
public class Dog{
public string Name { get; set;}
public int Age{ get; set;}
}
Is there any way how to set the default serialization method without having to change the method to:
public string GetDog(){
return dog.toString();
}
thanks
Assuming you are talking about .net web services. Here is the trick.
[WebMethod]
public Dog GetDog()
{
//method code
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public class Dog{
public string Name { get; set;}
public int Age{ get; set;}
}
You can read this article for further understanding.
http://ryanfarley.com/blog/archive/2004/05/26/737.aspx

Does mapping a child class in EF 4.1 require an ID property?

I am starting to move some code over to EF4.1 and I am having a problem loading child classes.
I found this article and it looks like there's a way to map the child class but I was wondering if the ID columns in the parent class are required.
I thought it was "no code" to simply include a reference to the child class and the rest was taken care of.
Here's the current object model:
public class classMember
{
public int MemberID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
public class classReservation
{
public int ReservationID {get; set;}
public classMember Member {get; set;}
}
But loading the classReservation just gives null Members.
Do I need to include a property for the MemberID in classReservation as well as in classMember? It seems redundant from a design point of view.
First of all, in order to have navigation properties from one entity to another, they must be declared as virtual in your POCO. So you would want this:
public class classReservation
{
public int ReservationID {get; set;}
public virtual classMember Member {get; set;}
}
This is because at runtime, EF actually subclasses your POCO using reflection. To make the navigation property work, it needs to be able to override it. This is why it has to be virtual.
To answer your second question, no, you do not need to have a "foreign key property" from the child entity to the parent entity. It helps EF, but is not necessary.
We are in the process of removing foreign key properties from our entity models. To do it, you still need to tell EF how to map the relationship in the db. This can be done in your DbContext class' OnModelCreating method:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<classReservation>
.HasRequired(r => r.classMember)
.WithMany()
.Map(x => x.MapKey("MemberId"));
}
You could also use HasOptional, WithRequiredDependent, etc, depending on the cardinality and multiplicity of the relationship.

Resources