2

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?

linuxod
  • 77
  • 1
  • 7
  • Are you getting an error at all? – Corey Adler Dec 18 '12 at 20:01
  • I get an Exception "Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.User_0AFA654F65C3FB4CE9ADD687E486B5EB3BE472E040A3B97DA79144EA5F3D75AC'. Path 'rows[0].Developer.Roles[0].Users'." – linuxod Dec 18 '12 at 20:26

2 Answers2

3

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.

DbContext.Configuration.ProxyCreationEnabled = false;

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.

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • I'm sorry, I didn't provided more specific info. You are right, I have problems with JsonConvert.SerializeObject() method (from NewtonSoft.Json), all I'm trying to do - is to build data on server-side and give it to the jqGrid.DbContext.Configuration.ProxyCreationEnabled = false;didn't made any result, I get null on my foreign key. I'll read XY Problem link you provided and give an answer a little bit later. Thanks. – linuxod Dec 18 '12 at 22:16
  • Trying turning off `ProxyCreationEnabled` and then you'll probably have to write your own [JsonConverter](http://james.newtonking.com/projects/json/help/index.html?topic=html/T_Newtonsoft_Json_JsonConverter.htm). – Erik Philips Dec 18 '12 at 22:21
0

I have a very similar structure in my application that also has Users and Roles. What I did for my application (and this might be the answer for yours), is that I have a 3rd object, the UserRole, which makes a link-table between the User and Role tables. The only items in the UserRole object are the FKs to the User and Role table. Then switch User to having a List<UserRole> Roles and you can remove the User reference from the Role object.

Let me know if that works.

The code:

public class UserRole
{
    [Key, ForeignKey("User"), ColumnAttribute(Order=0)]
    public int UserID { get; set; }

    [Key, ForeignKey("Role"), ColumnAttribute(Order = 1)]
    public int RoleID { get; set; }

    public virtual GaiaUser User { get; set; }

    public virtual Role Role { get; set; }
}
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
  • Just tryed to make that layer between Users and Roles. Unfortunately it didn't the trick. I wrote in my question post that I'm using another Membership Provider, which is already using Roles and Users standard fields. – linuxod Dec 18 '12 at 20:52