[英]Entity having child collections causing issues in WCF : Consider using a DataContractResolver if you are using DataContractSerializer
Have been struggling to fix this for a day now. 每天都在努力解决此问题。 New to EF as well !! EF的新手!!
I require to get my product list together with its child collections. 我需要将我的产品列表及其子集合一起获取。
My Model.tt is in a DataModel.dll and I have reference this in both WCF service and my client. 我的Model.tt在DataModel.dll中,并且在WCF服务和我的客户端中都已引用了它。 When the entity has child collection, I am getting this error. 当实体具有子集合时,出现此错误。 Guess it is due to the public virtual ICollection of the entity. 猜猜这是由于实体的公共虚拟ICollection所致。 How can I fix this ? 我怎样才能解决这个问题 ?
Trying to put [KnownType(typeof(entity.Name))] in my Model.tt will help ? 尝试将[KnownType(typeof(entity.Name)))放入Model.tt中会有所帮助吗? If so how to get the entity.Name to be passed generically in my Model.tt ? 如果是这样的话,如何在我的Model.tt中通用地传递entity.Name?
TRIED : 尝试:
ERROR CAPTURED IN TRACE LOG: 跟踪日志中捕获的错误:
Type 'System.Data.Entity.DynamicProxies.Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924' with data contract name 'Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies ' is not expected. :键入“System.Data.Entity.DynamicProxies.Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924”数据合同名称“Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924 http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies预计不会”。 Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer. 如果您正在使用DataContractSerializer或将任何不是静态已知的类型添加到已知类型的列表中,请考虑使用DataContractResolver-例如,通过使用KnownTypeAttribute属性或将它们添加到传递给序列化程序的已知类型的列表中。
There was an error while trying to serialize parameter http://tempuri.org/:GetAllProductResult . 尝试序列化参数http://tempuri.org/:GetAllProductResult时发生错误。 The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924' with data contract name 'Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies ' is not expected. 该消息的InnerException是“类型‘System.Data.Entity.DynamicProxies.Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924’数据合同名称‘Entity_Product_F540EDA252AD69FEA102E0C9AB0167D5397996ADA4679FA9C4089B58B9766924: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies预计不会’。 Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.'. 如果您正在使用DataContractSerializer或将任何静态未知的类型添加到已知类型的列表中,请考虑使用DataContractResolver-例如,通过使用KnownTypeAttribute属性或将它们添加到传递给序列化程序的已知类型的列表中。 Please see InnerException for more details. 有关更多详细信息,请参见InnerException。
AUTO GENERATED ENTITY : 自动产生的实体:
[Serializable]
public partial class Entity_Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Entity_Product()
{
this.tbl_ManufacturerDetail = new HashSet<Entity_ManufacturerDetail>();
this.tbl_ProductDetails = new HashSet<Entity_ProductDetails>();
}
public int ProductId { get; set; }
public string Administration { get; set; }
public string Manufacturer { get; set; }
public Nullable<decimal> Price { get; set; }
public Nullable<bool> IsEnabled { get; set; }
public Nullable<System.DateTime> CreatedOn { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> UpdatedOn { get; set; }
public string UpdatedBy { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Entity_ManufacturerDetail> tbl_ManufacturerDetail { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Entity_ProductDetails> tbl_ProductDetails { get; set; }
}
I'm not sure if this is the solution to your problem but this will let you add [KnownType(typeof(entity.Name))]
in your Model.tt
. 我不确定这是否是解决您问题的方法,但这将使您可以在Model.tt
添加[KnownType(typeof(entity.Name))]
。 Not exactly the entity name but the entity names of each collection navigation property. 不完全是实体名称,而是每个集合导航属性的实体名称。
This is line 1-70 of my modified Model.tt
file. 这是修改后的Model.tt
文件的1-70行。 I only changed the lines between the comments //START MODIFICATION
and //END MODIFICATION
. 我只更改了注释//START MODIFICATION
和//END MODIFICATION
之间的行。
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF6.Utility.CS.ttinclude"#><#@
output extension=".cs"#><#
const string inputFile = @"Model.edmx";
var textTransform = DynamicTextTransformation.Create(this);
var code = new CodeGenerationTools(this);
var ef = new MetadataTools(this);
var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
var fileManager = EntityFrameworkTemplateFileManager.Create(this);
var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
{
return string.Empty;
}
WriteHeader(codeStringGenerator, fileManager);
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
fileManager.StartNewFile(entity.Name + ".cs");
BeginNamespace(code);
#>
//START MODIFICATION
using System.Runtime.Serialization;
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#
var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
var complexProperties = typeMapper.GetComplexProperties(entity);
foreach (var navigationProperty in collectionNavigationProperties)
{
#>
[KnownType(typeof(<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>))]
<#
}
#>
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
<#
if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
{
#>
public <#=code.Escape(entity)#>()
{
//END MODIFICATION
<#
foreach (var edmProperty in propertiesWithDefaultValues)
{
#>
this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
<#
}
foreach (var navigationProperty in collectionNavigationProperties)
{
#>
this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
<#
}
foreach (var complexProperty in complexProperties)
{
#>
this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
}
#>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.