开发者

One-to-Many Relationship in Entity Framework 4.1: foreign Id is retrieved, but the object itself not

I'm using Entity Framework 4.1 and trying to create a one-to-many relationship. In my software I have UserEntity and RoleEntity classes. Each User has one Role but one Role can have many Users.

So far I've succeeded in getting the data from the DB to the UserEntity except for the related RoleEntity object. The RoleId property of my test user object has the correct RoleID but Role property is nul开发者_如何学运维l.

Is there some kind of additional configuration that I would need to write before the Role property is populated with the correct RoleEntity?

[Table("Roles")]
public class RoleEntity
{
    public long Id { get; set; }
    public string Name { get; set; }
}

[Table("Users")]
public class UserEntity
{
    public long Id { get; set; }
    public string Password { get; set; }
    public Int32 IsActive { get; set; }

    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string LocalService { get; set; }

    [ForeignKey("RoleEntity")]
    public long RoleId { get; set; }

    public virtual RoleEntity Role { get; set; }
    public virtual ClientOrganizationEntity ClientOrganization { get; set; }
}

public class UserContext : DbContext
{
    #region Constructor

    public UserContext(DbConfigurationProvider dbConfigurationProvider)
        : base(dbConfigurationProvider.ConnectionString)
    {
        if (dbConfigurationProvider == null)
            throw new ArgumentNullException("dbConfigurationProvider");

        Configuration.ProxyCreationEnabled = false;
    }

    #endregion

    #region DbSets

    public DbSet<UserEntity> Users { get; set; }
    public DbSet<RoleEntity> Roles { get; set; }
    public DbSet<ClientOrganizationEntity> ClientOrganizations { get; set; }

    #endregion
}

Data in Users table:

Id User Password FirstName LastName Email LocalService IsActive RoleId ClientOrganizationId

1 foo 62cdb7020ff920e5aa642c3d4066950dd1f01f4d FirstName LastName te@s.t foobar.com 1 1 1

Data in Roles table:

Id Name

1 Administrator

EDIT:

This how I get the data from the DbContext:

var user = _dbContext.Users.FirstOrDefault(x => x.Id == 1);
var role = user.Role;


I didn't see that you have disabled proxy creation in the constructor by

Configuration.ProxyCreationEnabled = false

Lazy loading will not work when you disable Proxy creation.

So either Enable proxy creation or use Include method to eager load the navigational properties

var user = _dbContext.Users.Include(u => u.Role).FirstOrDefault(x => x.Id == 1);


You need to enable lazy loading Configuration.LazyLoadingEnabled=true; in your UserContext constructor.


I ended up solving this myself. For some disabling of Configuration.ProxyCreationEnabled broke the relationships. The only change I made to fix it was remove this line:

Configuration.ProxyCreationEnabled = false;

Surely relationships must be possible to be used even with proxy creation disabled so if anyone has any additional information, I'd like to hear it. For the time being however, I'm just glad that the problem was solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜