【问题标题】:EF 5.0 not removing optional one to one navigation propertyEF 5.0 不删除可选的一对一导航属性
【发布时间】:2013-01-30 22:30:35
【问题描述】:

在 Entity Framework 5 中将可选的一对一导航属性设置为 null 似乎并没有进入数据库。这是预期的行为吗?

在下面的例子中,Person 是一个代理对象。我希望将地址设置为 null 会导致该地址从数据库中删除。

如果我在将地址设置为空之前延迟加载地址,则下面的代码可以工作。但是加载之前的地址

任何帮助将不胜感激。

namespace ConsoleApplication2
{
using System;
using System.Data.Entity;

internal class Program
{
    private static void Main(string[] args)
    {
        using (PersonContext context = new PersonContext())
        {
            // Make sure person with Id = 1 exists with an address.
            Person person = context.People.Find(1) ?? context.People.Add(new Person { Id = 1 });
            if (person.Address == null)
            {
                person.Address = new Address
                    {
                        Street = "123 Main Street",
                        City = "SomeCity",
                        State = new State
                        {
                            Code = "NY",
                            Name = "New York"
                        },
                        Zip = "11771"
                    };
            }
            context.SaveChanges();
        }

        // Setting address to null should remove relationship 
        using (PersonContext context = new PersonContext())
        {
            Person person = context.People.Find(1);

            Console.WriteLine("Person is a " + person.GetType());

            person.Address = null;
            context.SaveChanges();

            if (person.Address == null)
            {
                Console.WriteLine("Success: Person.Address is null.");
            }
            else
            {
                Console.WriteLine("Failure: Person.Address is not null.");
            }
        }
    }
}

public class Person
{
    public int Id { get; set; }
    public virtual Address Address { get; set; }
}

public class Address
{
    public int Person_Id { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public int StateId { get; set; }
    public State State { get; set; }
    public string Zip { get; set; }
}

public class State
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

public class PersonContext : DbContext
{
    public DbSet<Person> People { get; set; }

    public DbSet<State> States { get; set; }

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

        modelBuilder.Entity<Address>()
            .HasKey(x => x.Person_Id);

        modelBuilder.Entity<Person>()
            .HasOptional<Address>(x => x.Address)
            .WithRequired()
            .WillCascadeOnDelete();
    }
}

}

【问题讨论】:

    标签: entity-framework lazy-loading code-first


    【解决方案1】:

    在你没有使用延迟加载的地方,地址等相关属性将不会被加载,因此已经为空。

    为了确保它总是被加载使用急切加载:

    Person person = context.People.Include(x => x.Address).Single(x => x.Id == 1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-30
      • 2022-07-18
      • 1970-01-01
      • 2012-07-06
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多