gpt4 book ai didi

sql-server - 如何在 SqlServer Compact 数据库上执行 Entity Framework 模型第一个 DDL 文件?

转载 作者:行者123 更新时间:2023-12-02 22:43:31 25 4
gpt4 key购买 nike

这是一些看起来很简单但几天以来让我发疯的东西......

我正在尝试使用 Entity Framework 和 Sql Server Compact 创建一个应用程序。我想使用模型优先方法。我为模型创建了一个 edmx 文件,并运行了数据库生成向导,该向导生成了一个包含表创建命令的 edmx.sqlce 文件。但是在哪里运行这个 edmx.sqlce 文件呢?

我尝试通过将内容发送到 SqlCommand 的 ExecuteNonQuery 命令来运行该文件。但它也会给出语法错误 - 但为什么生成的 edmx.sqlce 文件会损坏???我想我必须在其他地方运行该文件。谁能告诉我在哪里? (我只有 C# Express 版本)。

edmx.sqlce 文件的内容是:

    -- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server Compact Edition
-- --------------------------------------------------
-- Date Created: 07/27/2011 23:50:28
-- Generated from EDMX file: C:\Users\Ralf\Programmierung\MySoftware\MySoftware\MySoftwareDatenbank\MySoftwareDataModel.edmx
-- --------------------------------------------------


-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- NOTE: if the constraint does not exist, an ignorable error will be reported.
-- --------------------------------------------------


-- --------------------------------------------------
-- Dropping existing tables
-- NOTE: if the table does not exist, an ignorable error will be reported.
-- --------------------------------------------------

DROP TABLE [DataSourceSet];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'DataSourceSet'
CREATE TABLE [DataSourceSet] (
[Id] int IDENTITY(1,1) NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'DataSourceSet'
ALTER TABLE [DataSourceSet]
ADD CONSTRAINT [PK_DataSourceSet]
PRIMARY KEY ([Id] );
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------

我的环境是:- SqlServerCompact 3.5- Visual C# 2010 Express

最佳答案

您不必手动运行 DDL 脚本,它应该在您第一次在与所需数据库连接正确关联的代码中创建 DbContext 时自动完成。我对模型优先不像对代码优先那么熟悉,但我知道 EF 4.1 可以更好地支持 SQL Server CE 4。

如果出于某种原因,您仍然希望通过 ExecuteNonQuery 手动执行 DDL 脚本,则需要删除所有批处理终止符(“GO”),因为 ExecuteNonQuery 不支持批处理查询。

关于sql-server - 如何在 SqlServer Compact 数据库上执行 Entity Framework 模型第一个 DDL 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6852079/

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