毫无疑问,我在这里遗漏了一些明显的东西,所以也许互联网的集体力量可以向我指出。
我有一个代码优先的数据库结构,由用户、组和用于链接两者的 GroupUsers 表组成(EF 可以自己处理 nn 关系,但breathjs 明确需要该表https://stackoverflow.com/a/13636530/249813)。
然后我尝试在客户端通过 Breezejs 加载用户。当用户不是任何组的成员时,这可以正常工作,但当他们使用以下消息时会失败。
object #<object> has no method getproperty
谁能告诉我哪里出错了?
数据库:
public class MyContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<GroupUser> GroupUsers { get; set; }
}
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string Name { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public bool IsActive { get; set; }
public bool IsAdmin { get; set; }
public virtual ICollection<GroupUser> Groups { get; set; }
}
public class Group
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<GroupUser> Users { get; set; }
}
public class GroupUser
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("User")]
public Guid UserId { get; set; }
[ForeignKey("Group")]
public Guid GroupId { get; set; }
public virtual Group Group { get; set; }
public virtual User User { get; set; }
}
Javascript
var serviceName = '/breeze/admin';
var manager = new breeze.EntityManager(serviceName);
var query = breeze.EntityQuery.from('Users');
manager.executeQuery(query)
.then(function(data) {
//do something good
})
.fail(function(error) {
//do something bad.
});
当用户不是组成员时,Breeze API 控制器生成的 JSON(工作正常)
[
{
$id: "1",
$type: "System.Data.Entity.DynamicProxies.User_41E3B55B982835BEAAA863D9A28B60E1A07D42FBEB52862F05DCD4BAE3F6171C, EntityFrameworkDynamicProxies-MyBreezeTest",
Groups: [ ],
Id: "665b2a59-f1ae-41c6-8d6b-df758713db01",
Name: "Me",
Email: "me@example.com",
IsActive: true,
IsAdmin: true
}
]
当用户是组成员时,Breeze API 控制器生成的 JSON(失败)
[
{
$id: "1",
$type: "System.Data.Entity.DynamicProxies.User_41E3B55B982835BEAAA863D9A28B60E1A07D42FBEB52862F05DCD4BAE3F6171C, EntityFrameworkDynamicProxies-MyBreezeTest",
Groups: [
{
$id: "2",
$type: "System.Data.Entity.DynamicProxies.GroupUser_144E644846F6E074946B6DFE31A643AFBBDA0EF263BEBCAA9A6A12E67649A7CF, EntityFrameworkDynamicProxies-MyBreezeTest",
Group: {
$id: "3",
$type: "System.Data.Entity.DynamicProxies.Group_4EDDE8FACA0560ADBB390090E3A6A147D06867A4A693B111AECF35D2CE7E458C, EntityFrameworkDynamicProxies-MyBreezeTest",
Users: [
{
$ref: "2"
}
],
Id: "86365a48-6f45-4614-b0b0-8011ec0e0d77",
Name: "Test Group"
},
User: {
$ref: "1"
},
Id: "952faa7b-658d-4e12-8f18-dc9b0898d684",
UserId: "665b2a59-f1ae-41c6-8d6b-df758713db01",
GroupId: "86365a48-6f45-4614-b0b0-8011ec0e0d77"
}
],
Id: "665b2a59-f1ae-41c6-8d6b-df758713db01",
Name: "Me",
Email: "me@example.com",
IsActive: true,
IsAdmin: true
}
]
为未来的读者编辑
下面 Margabit 的回答正确识别了问题,但是为了便于参考,解决此问题的最简单方法不是在微风控制器中执行此操作:
var context = new MyContext();
而是这样做
var context = _myEFContextProvider.Context;
由于 EFContextProvider 将在它自己的实例上禁用代理创建和延迟加载,并允许您保持虚拟并避免在上下文构造函数中全局禁用延迟加载。