1

我想删除数据库中的一行并使用相同的 Id 再次插入它,这听起来很荒谬,但这是场景:

域类如下:

public class SomeClass
{
    public int SomeClassId { get; set; }
    public string Name { get; set; }
    public virtual Behavior Behavior { get; set; }
}

public abstract class Behavior
{
    public int BehaviorId { get; set; }
}

public class BehaviorA : Behavior
{
    public string BehaviorASpecific { get; set; }
}

public class BehaviorB : Behavior
{
    public string BehaviorBSpecific { get; set; }
}

实体上下文是

public class TestContext : DbContext
{
    public DbSet<SomeClass> SomeClasses { get; set; }
    public DbSet<Behavior> Behaviors { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        modelBuilder.Entity<SomeClass>()
            .HasOptional(s => s.Behavior)
            .WithRequired()
            .WillCascadeOnDelete(true);

    }
}

现在可以执行这段代码来演示这一点(在下面的代码中用注释描述)

    using(TestContext db = new TestContext())
    {
        var someClass = new SomeClass() { Name = "A" };
        someClass.Behavior = new BehaviorA() { BehaviorASpecific = "Behavior A" };
        db.SomeClasses.Add(someClass);

        // Here I have two classes with the state of added which make sense
        var modifiedEntities = db.ChangeTracker.Entries()
                                 .Where(entity => entity.State != System.Data.Entity.EntityState.Unchanged).ToList();
        // They save with no problem
        db.SaveChanges();

        // Now I want to change the behavior and it causes entity to try to remove the behavior and add it again
        someClass.Behavior = new BehaviorB() { BehaviorBSpecific = "Behavior B" };

        // Here it can be seen that we have a behavior A with the state of deleted and 
        // behavior B with the state of added
        modifiedEntities = db.ChangeTracker.Entries()
                 .Where(entity => entity.State != System.Data.Entity.EntityState.Unchanged).ToList();

        // But in reality when entity sends the query to the database it replaces the 
        // remove and insert with an update query (this can be seen in the SQL Profiler) 
        // which causes the discrimenator to remain the same where it should change.
        db.SaveChanges();
    } 

如何更改此实体行为以便删除和插入而不是更新?

4

1 回答 1

0

一个可能的解决方案是在 2 个不同的步骤中进行更改:someClass.Behavior = new BehaviorB() { BehaviorBSpecific = "Behavior B" };插入之前

someClass.Behaviour = null;
db.SaveChanges();

该行为与数据库模型有关。EF 中的 BehaviourA 和 B 与同一个 EntityRecordInfo 相关,具有相同的 EntitySet(行为)。如果您在上下文中创建 2 个不同的 DbSet,您也有相同的行为,因为 DB 模型保持不变。

编辑
实现类似 1-1 关系结果的另一种方法是使用 ComplexType。它们也适用于继承。这里有一个例子

public class TestContext : DbContext
{
    public TestContext(DbConnection connection) : base(connection, true) { }

    public DbSet<Friend> Friends { get; set; }
    public DbSet<LessThanFriend> LessThanFriends { get; set; }
}

public class Friend
{
    public Friend()
    {Address = new FullAddress();}

    public int Id { get; set; }
    public string Name { get; set; }

    public FullAddress Address { get; set; }
}

public class LessThanFriend
{
    public LessThanFriend()
    {Address = new CityAddress();}

    public int Id { get; set; }
    public string Name { get; set; }

    public CityAddress Address { get; set; }
}


[ComplexType]
public class CityAddress
{
    public string Cap { get; set; }
    public string City { get; set; }
}

[ComplexType]
public class FullAddress : CityAddress
{
    public string Street { get; set; }
}
于 2015-08-13T08:29:34.227 回答