can’t find any matching solution.
Let’s head to the point: MVC4 application, EF and CodeFirstSharpMembership provider.
There is an entity
public class User
{
...
public virtual ICollection<Role> Roles { get; set; }
}
And standard Role Entity:
public class Role
{
...
public virtual ICollection<User> Users { get; set; }
}
And of course when I try to make my own Entity with FK to User
public class MyEntity
{
...
public virtual User Developer { get; set; }
}
I always get Self-Referrence loop because User reffers to Roles, and Roles reffers to User.
Then I tryed to
Context = new DataContext();
Context.Configuration.LazyLoadingEnabled = false;
To avoid selecting any Foreign Keys, and after selecting something
var Developers = Context.MyEntities;
And of course my IQueryable was without “Developer” field.
Then I tryed to:
var Developers = Context.MyEntities.Include("Developer");
And of course got Self-Referrence loop.
How can I keep selecting FKeys and exclude “Role” field from User?
There shouldn’t be a problem with self-referencing entities unless you are using a method that cannot handle the self-referencing (like serialization). If you are having serialization problems (I have to guess because you haven’t provided any what you are trying to produce the error message, most likely serialization or data binding I simply don’t know) so I would suggest:
Turning off Proxy object creation on your DbContext.
Typically this scenario is because the application is using POCO objects (Either T4 Generated or Code-First). The problem arises when Entity Framework wants to track changes in your object which is not built into POCO objects. To resolve this, EF creates proxy objects which lack the attributes in the POCO objects, and aren’t serializable.
I’d also add that you have an XY Problem. You are asking how to exclude a property(The Y Problem) because you think it will solve a problem (The X Problem) and it may or it may not. In actuality you need to state source problem (X) because there will be most likely a solution (like i’ve described) that doesn’t require (Y) at all.