POCO类未转换为代理

时间:2016-06-19 23:14:40

标签: c# entity-framework

我遇到了一个非常奇怪和脱发导致的错误。我已经使用EF和存储库模式多年了,但现在我得到了这个非常奇怪的错误:我的Telephone POCO类没有被转换为代理。每个其他POCO类都成为代理但Telephone。我删除并重新创建了该文件,存储库文件等等......我无法看到我在这里犯的错误(可能非常愚蠢)。这是我将其添加到上下文中的方式:

public virtual T Add()
{
    return DbSet.Add(DbSet.Create());
}

public virtual T Add(T entity)
{
    var temp = Add();
    PropertyCopy.CopyValues(entity, temp);

    return DbSet.Add(temp);
}

DbSet:DbContext.Set<T>()

在这里你可以看到差异:

不工作

enter image description here

enter image description here

工作

enter image description here

enter image description here

这是Telephone POCO类:

https://gist.github.com/eestein/ce6c453e806b1b1c6ed5ac43410f3687

这是EmployeeProfile POCO类(这个和所有其他类,成为代理):

https://gist.github.com/eestein/5a657ebe1ead739eaac5b00f2043bebe

这就是它们的创建方式:

var mobileTelephone = Context.Add(new Telephone
{
    AreaCode = model.ManagerAreaCode.Value,
    CountryCode = model.ManagerCountryCode.Value,
    Number = int.Parse(model.ManagerPhone),
    IsDefault = true,
    Type = TelephoneType.Mobile,
    IsValidated = LoggedUser?.IsSuperAdmin() ?? false
});
var profile = Context.Add(new EmployeeProfile
{
    Rank = EmployeeRank.Administrator
});

我感谢任何帮助,因为我几乎没有头发拉扯...

1 个答案:

答案 0 :(得分:1)

为什么需要实体框架来创建代理?我的猜测是你没有,因为你没有在电话课上使用延迟加载。您没有列出虚拟属性。在EmployeeProfile类中,你可以。

  

请注意,EF不会为有类型的类型创建代理   代理没什么可做的。这意味着你也可以避免   代理通过具有密封和/或没有虚拟的类型   属性。

https://msdn.microsoft.com/en-us/data/jj592886.aspx

相关问题