的EntityFramework的AddOrUpdate导致不正确的外键更新 [英] EntityFramework's AddOrUpdate leads to incorrect foreign key update

查看:211
本文介绍了的EntityFramework的AddOrUpdate导致不正确的外键更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些国家,每个都有多个城市。我可以用下面的模型来表示:

Suppose I have a number of countries, each of which has a number of cities. I can represent this using the following models:

public class Country
{
    public virtual int Id { get; set; }
    [Required]
    public virtual string Name { get; set; }

    public virtual ICollection<City> Cities { get; set; }
}

public class City
{
    public virtual int Id { get; set; }
    [Required]
    public virtual string Name { get; set; }

    public virtual Country Country { get; set; }
    public virtual int CountryId { get; set; }
}

class TestContext : DbContext
{
    public DbSet<City> Cities { get; set; }
    public DbSet<Country> Countries { get; set; }
}



实体框架正确识别外键和生成表。

Entity Framework correctly identifies the foreign keys and generates the tables.

不过,如果我现在试着种子一些测试数据:

However, if I now try to seed some test data:

static class Program
{
    static void Main()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TestContext>());
        using (var db = new TestContext())
        {
            db.Database.Initialize(true);
            db.Database.Log = Console.Write;

            var country = db.Countries.Create();
            country.Name = "France";
            db.Countries.AddOrUpdate(a => a.Name, country);

            var city = db.Cities.Create();
            city.Name = "Paris";
            city.Country = country;
            db.Cities.AddOrUpdate(q => q.Name, city);

            db.SaveChanges();
        }
    }
}

这是第一次运行中,所有是好的,并且在数据库中CountryId字段被设置正常。然而,当我运行它第二次, db.SaveChanges() attemps设定巴黎CountryId为0,这违反了非空的外键约束。

The first time this is run, all is fine, and the CountryId field in the database is set properly. However, when I run it a second time, db.SaveChanges() attemps to set the CountryId of Paris to 0, which violates the non-nullable foreign key constraint.

这个问题似乎是,尽管城市国家是跟踪变化的代理,在 CountryId 属性从未更新。更新它手动不利于这一点,虽然。

The problem seems to be that despite city and country being change-tracking proxies, the CountryId property is never updating. Updating it manually doesn't help this, though.

这是预期的行为,如果是这样,我该如何使用 AddOrUpdate 不改变这样吗?通过设置外键做的一切似乎并没有成为一种选择,因为这些都不是可用的第一次代码运行。

Is this the intended behaviour, and if so, how can I use AddOrUpdate without changing like this? Doing everything by setting foreign keys doesn't seem to be an option, as those aren't available the first time the code runs.

推荐答案

据我所知 AddOrUpdate 不支持导航属性。此外,当 AddOrUpdate 通过输入没有得到任何财产,将与物业默认值保存。

AFAIK AddOrUpdate doesn't support navigation properties. Also when AddOrUpdate doesn't get any property by input it will save it with default value of property.

在。你stiuation使用导航属性。

In your stiuation you use navigation property.

 city.Name = "Paris";
 city.Country = country;



AddOrUpdate 不分配 city.CountryId = country.Id 通过内部由于缺乏支持导航属性。所以,你的 city.CountryId 值变为0它是整数默认值。

But AddOrUpdate doesn't assign city.CountryId=country.Id by internally because of lack of support navigation property. So your city.CountryId value becomes 0 which is default value for int.

您应该自己指定的外键

 var city = db.Cities.Create();
 city.Name = "Paris";
 city.Country = country;
 db.Cities.AddOrUpdate(q => q.Name, city);



应该是这样的。

should be like this

 var city = db.Cities.Create();
 city.Name = "Paris";
 city.Country = country;
 //Assign foreign key by manually.
 city.CountryId = country.Id;
 db.Cities.AddOrUpdate(q => q.Name, city);



我没有测试它。

I didn't test it.

这篇关于的EntityFramework的AddOrUpdate导致不正确的外键更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆