Entity Framework Fluent API: IndexAnnotationSerializer 无法序列化 'IndexAttribute' 类型的对象。
Entity Framework Fluent API: An object of type 'IndexAttribute' cannot be serialized by the IndexAnnotationSerializer.
我正在使用 EF 6.1.3 并遇到此错误。
"An object of type 'IndexAttribute' cannot be serialized by the IndexAnnotationSerializer. Only 'IndexAnnotation' objects can be serialized."
这是配置文件之一
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Web;
using MasterDetails.Models;
namespace MasterDetails.DataLayer
{
public class InventoryItemConfiguration : EntityTypeConfiguration<InventoryItem>
{
public InventoryItemConfiguration()
{
Property(ii => ii.InventoryItemCode)
.HasMaxLength(15)
.IsRequired()
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("AK_InventoryItem_InventoryItemCode") { IsUnique = true }));
Property(ii => ii.InventoryItemName)
.HasMaxLength(80)
.IsRequired()
.HasColumnAnnotation("Index", new IndexAttribute("AK_InventoryItem_InventoryItemName") { IsUnique = true });
Property(ii => ii.UnitPrice)
.HasPrecision(18, 2);
}
}
}
替换以下代码:
.HasMaxLength(50)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("AK_InventoryItem_InventoryItemName", 2) { IsUnique = false }));
并删除 .IsRequired()
IndexAttribute有两个重载,第二个重载是顺序。
异常是由以下行引起的
.HasColumnAnnotation("Index", new IndexAttribute("AK_InventoryItem_InventoryItemName") { IsUnique = true });
您需要将 IndexAttribute
包裹在 IndexAnnotation
中(正如您已经对另一列 InventoryItemCode
所做的那样):
.HasColumnAnnotation("Index", new IndexAnnotation(
new IndexAttribute("AK_InventoryItem_InventoryItemName") { IsUnique = true }));
我正在使用 EF 6.1.3 并遇到此错误。
"An object of type 'IndexAttribute' cannot be serialized by the IndexAnnotationSerializer. Only 'IndexAnnotation' objects can be serialized."
这是配置文件之一
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Web;
using MasterDetails.Models;
namespace MasterDetails.DataLayer
{
public class InventoryItemConfiguration : EntityTypeConfiguration<InventoryItem>
{
public InventoryItemConfiguration()
{
Property(ii => ii.InventoryItemCode)
.HasMaxLength(15)
.IsRequired()
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("AK_InventoryItem_InventoryItemCode") { IsUnique = true }));
Property(ii => ii.InventoryItemName)
.HasMaxLength(80)
.IsRequired()
.HasColumnAnnotation("Index", new IndexAttribute("AK_InventoryItem_InventoryItemName") { IsUnique = true });
Property(ii => ii.UnitPrice)
.HasPrecision(18, 2);
}
}
}
替换以下代码:
.HasMaxLength(50)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("AK_InventoryItem_InventoryItemName", 2) { IsUnique = false }));
并删除 .IsRequired()
IndexAttribute有两个重载,第二个重载是顺序。
异常是由以下行引起的
.HasColumnAnnotation("Index", new IndexAttribute("AK_InventoryItem_InventoryItemName") { IsUnique = true });
您需要将 IndexAttribute
包裹在 IndexAnnotation
中(正如您已经对另一列 InventoryItemCode
所做的那样):
.HasColumnAnnotation("Index", new IndexAnnotation(
new IndexAttribute("AK_InventoryItem_InventoryItemName") { IsUnique = true }));