gpt4 book ai didi

c# - Entity Framework Code First 导航属性返回空字段

转载 作者:行者123 更新时间:2023-11-30 20:59:09 25 4
gpt4 key购买 nike

对于我第一次使用 EF 代码的程序。过去我使用 linq to SQL en EF DbFirst。检索主记录时,我无法使用导航属性加载子记录。我得到一个空的子记录,其中所有记录字段都是 0 或 null。

当我想应用预先加载时。 .Include(x=>x.......) 没有显示我的导航。

我设置了以下类:

public Record()
{
Shipping = new ShippingData();
Delivery = new DeliveryData();
Items = new List<Item>();
ImportDate = DateTime.Now;
}

[Key]
public int RecordId { get; set; }
public int ShippingId { get; set; }
public int DeliveryId { get; set; }
public DateTime ImportDate { get; set; }

public virtual ShippingData Shipping { get; set; }
public virtual DeliveryData Delivery { get; set; }
public virtual List<Item> Items { get; set; }
public virtual Collecting CollectingOrder { get; set; }

具有以下上下文:

public class TweemansContext : DbContext
{
public TweemansContext()
: base("xxxx")
{
}

public DbSet<Add.Record> Records { get; set; }
public DbSet<Add.ShippingData> Shipping { get; set; }
public DbSet<Add.DeliveryData> Delivery { get; set; }
public DbSet<Add.Item> ProductItems { get; set; }
public DbSet<Add.Kolli> Kolli { get; set; }
public DbSet<Add.Collecting> CollectingOrders { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Record>().HasRequired(s => s.Shipping)
.WithMany()
.HasForeignKey(s => s.ShippingId);
modelBuilder.Entity<Record>().HasRequired(d => d.Delivery)
.WithMany()
.HasForeignKey(d => d.DeliveryId);
modelBuilder.Entity<Record>().HasOptional(c => c.CollectingOrder)
.WithOptionalDependent(a => a.Record).Map(p => p.MapKey("CollectionID"));
}
}

交付类为公共(public)类,如下:

    public class DeliveryData
{
[Key]
public int DeliveryId { get; set; }
public virtual Record Record { get; set; }

....lots of public properties

现在,当我尝试使用延迟加载时,我使用以下代码将传递类的所有属性设为空:

using (TweemansContext context = new TweemansContext())
{
var imported = (from record in context.Records
where record.ImportDate.Year == date.Year && record.ImportDate.Month == date.Month && record.ImportDate.Day == date.Day
select record).ToList();

foreach (var Record in imported)
{
string email;
string telnr;
CustomerService service = new CustomerService(connectionString);
**string servicenr = Record.Delivery.AccountNumber.Substring(2, 8);**
service.GetUserData(servicenr, out email, out telnr);
Record.Delivery.Email = email;
Record.Delivery.TelephoneNbr = telnr;
}
context.SaveChanges();
}

在带有 ** 的行上,我的调试器告诉我交付存在,但它的所有属性都是空的。

当想要按如下方式应用包含时,.include 不显示我的导航:

    var imported = (from record in context.Records.Include(x=>x.)
where
record.ImportDate.Year == date.Year
&& record.ImportDate.Month == date.Month
&& record.ImportDate.Day == date.Day
select record).ToList();

我做错了什么,或者我误解了哪一部分??

最佳答案

您必须从Record 构造函数中删除导航引用 的初始化,空导航集合 的初始化是可以的:

Shipping = new ShippingData(); // remove this line
Delivery = new DeliveryData(); // remove this line

本质上,这些行用 ShippingDataDeliveryData 的构造函数设置的值“覆盖”加载的数据,可能是默认值 0

关于您的评论“.Include(x=>x......) 未显示我的导航”的旁注。您需要将 using System.Data.Entity; 放在代码文件的开头,以使 Include 扩展方法可用,该扩展方法采用 lambda 表达式作为参数。

关于c# - Entity Framework Code First 导航属性返回空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15590905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com