实体框架插入对象两次

时间:2016-09-23 20:14:22

标签: c# entity-framework entity-framework-6

我遇到了Entity Framework 6的问题。看来EF正在尝试两次插入同一个对象。我已经审查了很多关于此的帖子,并尝试格式化我的解决方案,以配合这些帖子中的许多建议。此时,我的服务是创建一个新属性并为其相关属性定义分配外键。然后,该服务调用一个存储库方法,该方法创建一个新的上下文,将新创建的属性添加到上下文并尝试保存哪个索引违规失败(如果删除了索引,EF将插入两个相同的属性记录)。

我还尝试创建一个单独的存储库方法,在新的上下文中返回相应的Attribute Definition对象,将返回的Attribute Definition Object附加到Attribute w /指定的外键。这导致EF为每个属性创建新的属性定义(即使它们已经存在于数据库中)并插入与新属性定义相关的新属性。

仅在尝试插入多个属性时才会出现此问题。如果我通过传递单个属性来运行服务,它将在所有代码路径上正确处理。

另外注意,以下测试方法正确运行,但是如上所述,相同的代码被复制到它因索引违规而失败的服务中:

测试方法

[TestMethod]
public void Attribute()
{
    try
    {
        using (context)
        {
            List<Domain.Attribute> attributeList = new List<Domain.Attribute>();

            AttributeDefinition attributeDefinitionOne = context.AttributeDefinition.FirstOrDefault(m => m.AttributeDefinitionId == 1);

            Domain.Attribute attributeOne = new Domain.Attribute
            {
                AttributeDefinitionId = 1,
                ObjectId = 10074,
                ObjectType = "CD",
                AttributeValue = "Test Business Definition",
                CreateDate = DateTime.Now,
                CreateUser = Environment.UserName,
                LastModifiedDate = DateTime.Now,
                LastModifiedUser = Environment.UserName,
            };

            attributeList.Add(attributeOne);

            AttributeDefinition attributeDefintionTwo = context.AttributeDefinition.FirstOrDefault(m => m.AttributeDefinitionId == 2);

            Domain.Attribute attributeTwo = new Domain.Attribute
            {
                AttributeDefinitionId = 2,
                ObjectId = 10074,
                ObjectType = "CD",
                AttributeValue = "Test Stakeholder Definition",
                CreateDate = DateTime.Now,
                CreateUser = Environment.UserName,
                LastModifiedDate = DateTime.Now,
                LastModifiedUser = Environment.UserName
            };

            attributeList.Add(attributeTwo);

            foreach (Domain.Attribute attribute in attributeList)
            {
                //context.AttributeDefinition.Attach(attribute.AttributeDefinition);
                context.Attribute.Add(attribute);
                context.SaveChanges();

            }


            Domain.Attribute businessAttribute = context.Attribute.FirstOrDefault(m => m.AttributeDefinitionId == 1 && m.ObjectId == 10074 && m.ObjectType == "CD");
            Domain.Attribute stakeAttribute = context.Attribute.FirstOrDefault(m => m.AttributeDefinitionId == 2 && m.ObjectId == 10074 && m.ObjectType == "CD");

            Assert.IsNotNull(businessAttribute);
            Assert.IsNotNull(stakeAttribute);


        }
    }
    catch (Exception)
    {

        throw;
    }

}

属性对象

public class Attribute
{
    public int AttributeId { get; set; }
    public int AttributeDefinitionId { get; set; }
    public int ObjectId { get; set; }
    public string ObjectType { get; set; }
    public string AttributeValue { get; set; }
    public DateTime CreateDate { get; set; }
    public string CreateUser { get; set; }
    public DateTime LastModifiedDate { get; set; }
    public string LastModifiedUser { get; set; }

    public virtual AttributeDefinition AttributeDefinition { get; set; }

}

属性定义对象

public class AttributeDefinition
{
    public AttributeDefinition()
    {
        Attributes = new List<Domain.Attribute>();
    }
    public int AttributeDefinitionId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime CreateDate { get; set; }
    public string CreateUser { get; set; }
    public DateTime LastModifiedDate { get; set; }
    public string LastModifiedUser { get; set; }

    public virtual ICollection<Attribute> Attributes { get; set; }
}

属性EF制图

public AttributeMap() {
    ToTable("Attribute");
    HasKey(a => a.AttributeId);

    Property(a => a.AttributeId)
        .HasColumnName("AttributeId")
        .HasColumnType("INT");

    Property(a => a.AttributeDefinitionId)
        .HasColumnName("AttributeDefinitionId")
        .HasColumnType("INT");

    Property(a => a.ObjectId)
        .HasColumnName("ObjectId")
        .HasColumnType("INT");

    Property(a => a.ObjectType)
        .HasColumnName("ObjectType")
        .HasColumnType("VARCHAR")
        .HasMaxLength(2);

    Property(a => a.AttributeValue)
        .HasColumnName("AttributeValue")
        .HasColumnType("VARCHAR");

    Property(a => a.CreateDate)
        .HasColumnName("CreateDate")
        .HasColumnType("DATETIME");

    Property(a => a.CreateUser)
        .HasColumnName("CreateUser")
        .HasColumnType("VARCHAR");

    Property(a => a.LastModifiedDate)
        .HasColumnName("LastModifiedDate")
        .HasColumnType("DATETIME");

    Property(a => a.LastModifiedUser)
        .HasColumnName("LastModifiedUser")
        .HasColumnType("VARCHAR");

    HasRequired(attribute => attribute.AttributeDefinition)
        .WithMany(attributeDefinition => attributeDefinition.Attributes)
        .HasForeignKey(attribute => attribute.AttributeDefinitionId);

属性定义EF映射

ToTable("AttributeDefinition");
HasKey(a => a.AttributeDefinitionId);

Property(a => a.AttributeDefinitionId)
    .HasColumnName("AttributeDefinitionId")
    .HasColumnType("INT");

Property(a => a.Name)
    .HasColumnName("Name")
    .HasColumnType("VARCHAR");

Property(a => a.Description)
    .HasColumnName("Description")
    .HasColumnType("VARCHAR");

Property(a => a.CreateDate)
    .HasColumnName("CreateDate")
    .HasColumnType("DATETIME");

Property(a => a.CreateUser)
    .HasColumnName("CreateUser")
    .HasColumnType("VARCHAR");

Property(a => a.LastModifiedDate)
    .HasColumnName("LastModifiedDate")
    .HasColumnType("DATETIME");

Property(a => a.LastModifiedUser)
    .HasColumnName("LastModifiedUser")
    .HasColumnType("VARCHAR");

HasMany(attributeDefintion => attributeDefintion.Attributes)
    .WithRequired(attribute => attribute.AttributeDefinition)
    .HasForeignKey(attribute => attribute.AttributeDefinitionId);

服务方式:

public void SaveAttribute(string objectId, string objectType, IEnumerable<AttributeContract> attributes)
{

    //List of attribute definition Id
    List<int> attributeDefinitionIdList = new List<int>();
    //List<Domain.Attribute> attributeList = new List<Domain.Attribute>();

    foreach (AttributeContract attributeContract in attributes)
    {
        int iAttributeDefinitionId = Convert.ToInt32(attributeContract.AttributeDefinitionId);
        attributeDefinitionIdList.Add(iAttributeDefinitionId);

        int iObjectId = Convert.ToInt32(attributeContract.ObjectId);
        DateTime dCreateDate = Convert.ToDateTime(attributeContract.CreateDate);
        DateTime dLastModifiedDate = Convert.ToDateTime(attributeContract.LastModifiedDate);

        Domain.Attribute currentAttibute = new Domain.Attribute
        {
            AttributeDefinitionId = iAttributeDefinitionId,
            ObjectId = iObjectId,
            ObjectType = attributeContract.ObjectType,
            AttributeValue = attributeContract.AttributeValue,
            CreateDate = dCreateDate,
            CreateUser = attributeContract.CreateUser,
            LastModifiedDate = dLastModifiedDate,
            LastModifiedUser = attributeContract.LastModifiedUser
        };

        _repo.SaveAttribute(currentAttibute);

        //attributeList.Add(currentAttibute);
    }

    //IEnumerable<AttributeDefinition> currentAttributeDefintiionList = _repo.GetAttributeDefinition(attributeDefinitionIdList);

    //foreach (AttributeDefinition attributeDefinition in currentAttributeDefintiionList)
    //{
    //    //Assign the attribute definition to the attribute
    //    var attribute = attributeList.FirstOrDefault(a => a.AttributeDefinitionId == attributeDefinition.AttributeDefinitionId);
    //    attribute.AttributeDefinition = attributeDefinition;
    //}

    //_repo.SaveAttribute(attributeList);

}

属性存储库

public void SaveAttribute(Domain.Attribute attribute)
{
    using (var context = new ShpoDataDictionaryContext(new ContextConfiguration()))
    {
        context.Attribute.Add(attribute);
        context.SaveChanges();
    }
}

0 个答案:

没有答案