gpt4 book ai didi

entity-framework - LinqPad + EF 4.1 + SQL Server CE

转载 作者:行者123 更新时间:2023-12-02 00:26:30 24 4
gpt4 key购买 nike

几天来我一直在考虑这个想法,但我找不到任何明确的证据表明这个想法行得通或行不通。

我想要做的是使用 LinqPad 查询创建一个 EF 4.1 代码优先数据库,该数据库具有 SQL Server CE 支持,位于与查询相同的目录中(可能与查询命名相同)。这样做的原因是为了快速建立一个数据库来存储查询结果以供进一步处理。

想法是将类定义存储在查询中,即(这些将适用于最新的 beta

public class User
{
public long Id { get; set; }
public string Name{ get; set; }
}

public class MyDBContext : DbContext
{
// note the overloaded constructor
// just pass in the LinqPad UserQuery connection
public MyDBContext(DbConnection connection) : base(connection, true)
{}
public DbSet<User> Users { get; set; }
}

然后在查询中:

#define NONEST
public Main()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDBContext>());
// pass in the current database connection to point EF to the right database
var context = new MyDBContext(this.Connection);
context.Users.Add(new User(){Name = "Test User"});
context.SaveChanges();

context.Users.Dump("Contents of Users table");
}

我在 LinqPad 中引用了 EntityFramework 4.2.0.0,但在我的查询中,我看不到 System.Data.Entity 命名空间。这是否表明存在其他问题?

我不确定我是否提供了足够的上下文或信息,所以如果您需要澄清,请告诉我。这可以做到吗? LinqPad 还不支持这个吗?

更新:

似乎 EF 无法映射嵌套类,而这正是 LinqPad 查询定义的类。我在这方面不是很有经验,但似乎我已经走到了死胡同。除非 Joe Albahari 能够以某种方式实现非嵌套用户类,否则这看起来行不通。有人有想法吗?

最终更新:

使用 latest beta ,这完全有效。它需要事先创建 .sdf 数据库文件,但所有其他操作都应该有效。很酷。谢谢 Joe Albahari!

最佳答案

下载the latest beta然后将以下指令添加到查询的开头:

#define NONEST

这将告诉 LINQPad 将您定义的类型向上移动,以便它们是非嵌套的。

关于entity-framework - LinqPad + EF 4.1 + SQL Server CE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8730225/

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