LinqPad + EF 4.1 + SQL服务器CE

发布于 2024-12-25 00:18:07 字数 1499 浏览 0 评论 0 原文

我已经考虑了这个想法几天了,但我找不到任何明确的证据表明这会或不会奏效。

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

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

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 能够以某种方式实现非嵌套用户类,否则看起来这不会起作用。

最终更新:

使用最新测试版,这完全有效,它需要事先创建 .sdf 数据库文件,但所有其他操作都应该有效。非常酷。谢谢乔·阿尔巴哈里!

I've been toying with the idea for a couple of days, but I can't find any definitive evidence to suggest this will or will not work.

What I want to do is use LinqPad queries to create a EF 4.1 code-first database with a SQL Server CE backing in the same directory as the query (possibly named the same as the query). The reason for this would be to quickly set up a database for storing query results for further processing.

The idea is to store the class definitions in the query, i.e. (These will work with the latest 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; }
}

Then in the query:

#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");
}

I've referenced EntityFramework 4.2.0.0 in LinqPad, but in my query, I can't see the System.Data.Entity namespace. Is this indicative of some other problem?

I'm not sure if I've given enough context or information, so let me know if you need clarification. Can this be done? Does LinqPad not support this yet?

Update:

It appears that EF can't map nested classes, which is exactly what LinqPad query defined classes are. I'm not very experienced in this context, but it seems that I've hit a dead end. Unless Joe Albahari can somehow get non-nested user classes implemented, it doesn't look like this will work. Anyone have ideas?

Final Update:

Using the latest beta, this completely works. It requires creating the .sdf database file beforehand, but all other operations should work. Very cool. Thanks Joe Albahari!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2025-01-01 00:18:07

下载最新的测试版,然后将以下指令添加到查询的开头:

#define NONEST

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

Download the latest beta and then add the following directive to the start of your query:

#define NONEST

This will tell LINQPad to move types that you define a level up, so that they're non-nested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文