如何更新EF 6中的外键 - 代码优先 [英] How to update foreign key in EF 6 - Code First

查看:316
本文介绍了如何更新EF 6中的外键 - 代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想解释一下:



我的实体

  public class Person 
{
public int Id {get;组; }
public string Name {get;组; }
public virtual国家国家{get;组; }
}

public class Country
{
public int Id {get;组; }
public string Name {get;组;
}

我的数据库



表国家有2条记录:


  1. Id = 1,Name = France

  2. Id = 2,Name = Canada

表1个记录的人员:


  1. Id = 1,Name = Nicolas,Country_Id = 1

我的代码

  //在MVC应用程序中,这些类已通过EF在上一页中检索。所以现在我们已经丢失了所有的代理信息
var p = new Person(){Id = 1,Name =Nicolas};

//更改国家/地区
p.Country = new Country(){Id = 2,Name =Canada};

//使用(var db = new TestDbContext())保存DB
中的所有数据
{
db.Persons.Attach(p); //重新连接到EF上下文
db.Entry< Person>(p).State = EntityState.Modified; //标记修改状态
db.SaveChanges(); //仅生成字段name
}

我的问题



当执行以前的代码时,生成的SQL从不包含person表中的country_Id字段。



我的不问题



我知道在一个EF上下文中执行所有这些代码行时,我将从ASP.Net MVC页面获取数据。
我也想避免检索现有数据并逐个修改每个字段

解决方案

通过重试@伊万解决方案,我首先能够做所需的。



这是修改完成:

  public class Person 
{
public int Id {get;组; }
public string Name {get;组; }
public virtual国家国家{get;组; }
public int Country_Id {get;组; }
}

// ...

//更改国家/地区
p.Country = new Country(){Id = 2,Name = 加拿大};
p.Country_Id = 2;

但现在,从数据库获取实体时,我收到异常。
使用此代码:

  //首先使用(var db = new TestDbContext()获取数据
) )
{
var p2 = db.Persons.First();
}

我收到以下SqlException:列名无效Country_Id1 / p>

有没有人有任何线索能够检索数据来更新外键?


I'm trying to update a foreign key in EF6 (Code First) in an ASP.Net MVC manner.

Let me explain :

My entities

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual Country Country { get; set; }
}

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

My database

Table Countries with 2 records :

  1. Id = 1, Name = France
  2. Id = 2, Name = Canada

Table People with 1 record :

  1. Id = 1, Name = Nicolas, Country_Id = 1

My code

  // In a MVC application, these class has been retrieved via EF in a previous page. So now, we've lost all "proxy" informations
  var p = new Person() { Id = 1, Name = "Nicolas" };

  // Change country
  p.Country = new Country() { Id = 2, Name = "Canada" };

  // Persist all in DB
  using (var db = new TestDbContext())
  {                
      db.Persons.Attach(p); // Reattach to EF context
      db.Entry<Person>(p).State = EntityState.Modified; // Flag modified state
      db.SaveChanges(); // Generate only modification on field "name"
  }

My issue

When the previous code is executed, the generated SQL never include the country_Id field from the person table.

My "not" issue

I know that it works perfectly when doing all these lines of codes in one EF context but in my case, I will have the data coming from my ASP.Net MVC page. I would also like to avoid to retrieve the existing data and modify each field one by one

解决方案

By retrying @ivan solution, I was first able to do what was wanted.

Here are the modifications done :

 public class Person
 {
     public int Id { get; set; }
     public string Name { get; set; }
     public virtual Country Country { get; set; }
     public int Country_Id { get; set; }
 }

 // ...

 // Change country
 p.Country = new Country() { Id = 2, Name = "Canada" };
 p.Country_Id = 2;

But now, I get an exception when getting entities from the database. Using this code :

 // Retrieve data first
 using (var db = new TestDbContext())
 {
      var p2 = db.Persons.First();
 }

I get the following SqlException : "Invalid column name 'Country_Id1'."

Does anyone have any clues to be able to retrieve data and to update the foreign key ?

这篇关于如何更新EF 6中的外键 - 代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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