Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net:更改外键的值不会更改导航值_C#_Asp.net_Asp.net Mvc 4 - Fatal编程技术网

C# asp.net:更改外键的值不会更改导航值

C# asp.net:更改外键的值不会更改导航值,c#,asp.net,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc 4,我有一本书的模型: public class Book { [Required] [Key] public int BookId { get; set; } [Required] [StringLength(60, MinimumLength = 3)] public string Title { get; set; } [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")] [Requi

我有一本书的模型:

public class Book
{
    [Required]
    [Key]
    public int BookId { get; set; }

    [Required]
    [StringLength(60, MinimumLength = 3)]
    public string Title { get; set; }

    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
    [Required]
    [StringLength(30)]
    public string Genre { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }
}
public class Transactions
{
    [Key]
    public int TransactionsId { get; set; }
    public DateTime BuyTime { get; set; }

    public int BookId { get; set; }
    [ForeignKey("BookId")]
    public Book book { get; set; }
}
以及包含对书本模型的引用的事务模型:

public class Book
{
    [Required]
    [Key]
    public int BookId { get; set; }

    [Required]
    [StringLength(60, MinimumLength = 3)]
    public string Title { get; set; }

    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
    [Required]
    [StringLength(30)]
    public string Genre { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }
}
public class Transactions
{
    [Key]
    public int TransactionsId { get; set; }
    public DateTime BuyTime { get; set; }

    public int BookId { get; set; }
    [ForeignKey("BookId")]
    public Book book { get; set; }
}
我的问题是,当我创建新事务并将BookId设置为“1”时,book属性保持为空:

Transactioncs transaction = new Transactioncs();

// Init the book of the transaction
transaction.BookId = 1;
据我所知,当我更改外键时,导航属性(在本例中是“book”)应该相应地更改(因为我的数据库中有一本id=1的书)。 有人能给我解释一下为什么导航属性没有改变吗


当您从数据库检索对象时,感谢EntityFramework使用
Book
属性。如果保存事务实例,然后从数据库检索,则从数据库检索对象时,EF将填充book属性。EntityFramework将填充
book
属性。如果保存事务实例,然后从数据库检索,EF将填充book属性。