Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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/3/templates/2.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# WPF、SQLite和延迟加载_C#_Wpf_Entity Framework_Sqlite_Lazy Loading - Fatal编程技术网

C# WPF、SQLite和延迟加载

C# WPF、SQLite和延迟加载,c#,wpf,entity-framework,sqlite,lazy-loading,C#,Wpf,Entity Framework,Sqlite,Lazy Loading,我正在开发一个WPF应用程序,其中包含用于本地数据存储的SQLite数据库。 我从NuGet安装了EntityFramework 6.1.3和System.Data.SQLite 1.0.105.1,并正确配置了App.config文件 这些是我的实体 public class Event { public Guid Id { get; set; } = Guid.NewGuid(); public DateTime Date { get; set; } public s

我正在开发一个WPF应用程序,其中包含用于本地数据存储的SQLite数据库。 我从NuGet安装了EntityFramework 6.1.3和System.Data.SQLite 1.0.105.1,并正确配置了App.config文件

这些是我的实体

public class Event
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public DateTime Date { get; set; }
    public string Description { get; set; }

    public virtual Guid ContractId { get; set; }
    public virtual Contract Contract { get; set; }
}

public class Contract
{
    public Guid Id { get; set; } = Guid.NewGuid();

    [Required]
    public string Code { get; set; }

    public string Description { get; set; }

    public virtual ICollection<Event> Events { get; set; }
}

尝试使用即时加载加载相关实体:

using (SqliteDbContext ctx = new SqliteDbContext())
{
    var e1 = ctx.Events.Include(x => x.Contract).FirstOrDefault();
    MessageBox.Show(e1.Contract.Id.ToString());
}

实体框架加载相关实体:

感谢您的回复,即时加载工作正常,但我想使用延迟加载…显然,SQLite不支持此功能,除非您明确禁用它:。还要注意,EF7不支持延迟加载导航属性:。因此,快速加载可能是您在这里的唯一选择。您确定SQlite不支持它吗?从这篇文章来看,SQLite似乎支持它。为了其他阅读本文的人的利益,延迟加载对我来说似乎工作得很好,你只需要记住将成员虚拟化,以便EF可以代理它们。
using (SqliteDbContext ctx = new SqliteDbContext())
{
    var e1 = ctx.Events.Include(x => x.Contract).FirstOrDefault();
    MessageBox.Show(e1.Contract.Id.ToString());
}