Chr*_*Dog 4 .net inheritance entity-framework savechanges table-per-type
我在我的数据模型中实现了一些每个类型的表继承(基本上有一个BaseEntity
类型,包含我的项目的所有基本信息和Employer
从BaseEntity
项目继承的类型).一切似乎都是正确设置的,当使用实体时(通过ADO.net数据服务或通过Linq到实体)我可以看到Employer
类型和事情看起来没问题.当我创建一个新Employer
实体并尝试保存它时,问题就开始了.
在看起来不是.AddToEmployer
项目的上下文中(仅和/ AddObject
或AddToBaseEntity
).
如果我使用AddObject("Employer", NewEmployer)
我得到和错误消息:
找不到EntitySet名称'DataEntities.Employer'.
如果我使用,AddToBaseEntity(NewEmployer)
我会收到以下错误消息:
无法确定相关操作的有效排序.由于外键约束,模型要求orstore生成的值可能存在依赖关系.
我是否错过了设置继承的步骤?是否有一些特定的方法来保存继承的对象?我究竟做错了什么?我认为基本问题是我应该有一个AddToEmployer
,我需要做些什么来暴露它?看起来很奇怪它不是一个选项,因为我可以在客户端看到雇主类型并且可以执行以下操作:
var NewEmployer = new Employer()
- 这似乎表明我可以看到雇主类型罚款.
小智 7
我的名字是Phani,我在ADO.NET数据服务团队工作.
这些ResolveName
和ResolveType
方法是帮助您自定义客户端在发送到服务器的有效负载中写入的类型信息,以及如何实现服务器的响应有效负载.
它们可以帮助您解决客户端上的类型,并且在许多场景中都很有用,例如:
ResolveName
用于在向服务器发出请求时更改我们在线路上放置的实体的名称.
考虑这个数据模型:在服务器上
public class Employee {
public int EmployeeID {get;set;}
public string EmployeeName {get;set;}
}
public class Manager:Employee {
public List<int> employeesWhoReportToMe {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
当您使用客户端处理Manager实体类型的实例时,在将更改提交到服务器时,我们希望当实体参与继承时,类型信息将出现在有效负载中.
context.AddObject("Employees",ManagerInstance ); <-- add manager instance to the employees set.
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
但是,当客户端序列化此有效负载时,它会将"Employee"作为类型名称,而不是服务器上预期的类型名称.因此,您必须在客户端上提供名称解析器,
context.ResolveName = delegate(Type entityType){
//do what you have to do to resolve the type to the right type of the entity on the server
return entityType.FullName;
}
Run Code Online (Sandbox Code Playgroud)
类型解析器以相同的方式使用.
context.ResolveType = delegate(string entitySetName){
//do what you have to do to convert the entitysetName to a type that the client understands
return Type.GetType(entitySetName);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13997 次 |
最近记录: |