0

我是初学者,刚刚开始尝试使用 Entity Framework 4.1 中的建模工具。

图片显示了我的数据库的图表,再往下是自动生成的脚本来创建我的数据库(SQL Server 2008)。

在此处输入图像描述

-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/25/2012 21:49:52
-- Generated from EDMX file: D:\Libraries\Documents\Visual Studio 2010\HomeControl\DAL\HomeControlModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [HomeControl];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CameraImage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ImageSet] DROP CONSTRAINT [FK_CameraImage];
GO
IF OBJECT_ID(N'[dbo].[FK_EventTypeAction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ActionSet] DROP CONSTRAINT [FK_EventTypeAction];
GO
IF OBJECT_ID(N'[dbo].[FK_UnitAction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ActionSet] DROP CONSTRAINT [FK_UnitAction];
GO
IF OBJECT_ID(N'[dbo].[FK_UserAction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ActionSet] DROP CONSTRAINT [FK_UserAction];
GO
IF OBJECT_ID(N'[dbo].[FK_ScheduleUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserSet] DROP CONSTRAINT [FK_ScheduleUser];
GO
IF OBJECT_ID(N'[dbo].[FK_UnitCommand]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CommandSet] DROP CONSTRAINT [FK_UnitCommand];
GO
IF OBJECT_ID(N'[dbo].[FK_Camera_inherits_Unit]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UnitSet_Camera] DROP CONSTRAINT [FK_Camera_inherits_Unit];
GO
IF OBJECT_ID(N'[dbo].[FK_Tellstick_inherits_Unit]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UnitSet_Tellstick] DROP CONSTRAINT [FK_Tellstick_inherits_Unit];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[UserSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserSet];
GO
IF OBJECT_ID(N'[dbo].[ActionTypeSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ActionTypeSet];
GO
IF OBJECT_ID(N'[dbo].[UnitSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UnitSet];
GO
IF OBJECT_ID(N'[dbo].[ImageSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ImageSet];
GO
IF OBJECT_ID(N'[dbo].[ScheduleSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ScheduleSet];
GO
IF OBJECT_ID(N'[dbo].[ActionSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ActionSet];
GO
IF OBJECT_ID(N'[dbo].[CommandSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CommandSet];
GO
IF OBJECT_ID(N'[dbo].[UnitSet_Camera]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UnitSet_Camera];
GO
IF OBJECT_ID(N'[dbo].[UnitSet_Tellstick]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UnitSet_Tellstick];
GO

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

-- Creating table 'UserSet'
CREATE TABLE [dbo].[UserSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Schedule_Id] int  NOT NULL
);
GO

-- Creating table 'ActionTypeSet'
CREATE TABLE [dbo].[ActionTypeSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'UnitSet'
CREATE TABLE [dbo].[UnitSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'ImageSet'
CREATE TABLE [dbo].[ImageSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Data] varbinary(max)  NOT NULL,
    [Camera_Id] int  NOT NULL
);
GO

-- Creating table 'ScheduleSet'
CREATE TABLE [dbo].[ScheduleSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [OneOff] datetime  NULL,
    [Cronexpression] nvarchar(max)  NULL
);
GO

-- Creating table 'ActionSet'
CREATE TABLE [dbo].[ActionSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Time] datetime  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [EventType_Id] int  NOT NULL,
    [Unit_Id] int  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'CommandSet'
CREATE TABLE [dbo].[CommandSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Unit_Id] int  NOT NULL
);
GO

-- Creating table 'UnitSet_Camera'
CREATE TABLE [dbo].[UnitSet_Camera] (
    [Login] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [URL] nvarchar(max)  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'UnitSet_Tellstick'
CREATE TABLE [dbo].[UnitSet_Tellstick] (
    [Id] int  NOT NULL
);
GO

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

-- Creating primary key on [Id] in table 'UserSet'
ALTER TABLE [dbo].[UserSet]
ADD CONSTRAINT [PK_UserSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ActionTypeSet'
ALTER TABLE [dbo].[ActionTypeSet]
ADD CONSTRAINT [PK_ActionTypeSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UnitSet'
ALTER TABLE [dbo].[UnitSet]
ADD CONSTRAINT [PK_UnitSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ImageSet'
ALTER TABLE [dbo].[ImageSet]
ADD CONSTRAINT [PK_ImageSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ScheduleSet'
ALTER TABLE [dbo].[ScheduleSet]
ADD CONSTRAINT [PK_ScheduleSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ActionSet'
ALTER TABLE [dbo].[ActionSet]
ADD CONSTRAINT [PK_ActionSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CommandSet'
ALTER TABLE [dbo].[CommandSet]
ADD CONSTRAINT [PK_CommandSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UnitSet_Camera'
ALTER TABLE [dbo].[UnitSet_Camera]
ADD CONSTRAINT [PK_UnitSet_Camera]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UnitSet_Tellstick'
ALTER TABLE [dbo].[UnitSet_Tellstick]
ADD CONSTRAINT [PK_UnitSet_Tellstick]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

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

-- Creating foreign key on [Camera_Id] in table 'ImageSet'
ALTER TABLE [dbo].[ImageSet]
ADD CONSTRAINT [FK_CameraImage]
    FOREIGN KEY ([Camera_Id])
    REFERENCES [dbo].[UnitSet_Camera]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CameraImage'
CREATE INDEX [IX_FK_CameraImage]
ON [dbo].[ImageSet]
    ([Camera_Id]);
GO

-- Creating foreign key on [EventType_Id] in table 'ActionSet'
ALTER TABLE [dbo].[ActionSet]
ADD CONSTRAINT [FK_EventTypeAction]
    FOREIGN KEY ([EventType_Id])
    REFERENCES [dbo].[ActionTypeSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventTypeAction'
CREATE INDEX [IX_FK_EventTypeAction]
ON [dbo].[ActionSet]
    ([EventType_Id]);
GO

-- Creating foreign key on [Unit_Id] in table 'ActionSet'
ALTER TABLE [dbo].[ActionSet]
ADD CONSTRAINT [FK_UnitAction]
    FOREIGN KEY ([Unit_Id])
    REFERENCES [dbo].[UnitSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UnitAction'
CREATE INDEX [IX_FK_UnitAction]
ON [dbo].[ActionSet]
    ([Unit_Id]);
GO

-- Creating foreign key on [User_Id] in table 'ActionSet'
ALTER TABLE [dbo].[ActionSet]
ADD CONSTRAINT [FK_UserAction]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[UserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserAction'
CREATE INDEX [IX_FK_UserAction]
ON [dbo].[ActionSet]
    ([User_Id]);
GO

-- Creating foreign key on [Schedule_Id] in table 'UserSet'
ALTER TABLE [dbo].[UserSet]
ADD CONSTRAINT [FK_ScheduleUser]
    FOREIGN KEY ([Schedule_Id])
    REFERENCES [dbo].[ScheduleSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ScheduleUser'
CREATE INDEX [IX_FK_ScheduleUser]
ON [dbo].[UserSet]
    ([Schedule_Id]);
GO

-- Creating foreign key on [Unit_Id] in table 'CommandSet'
ALTER TABLE [dbo].[CommandSet]
ADD CONSTRAINT [FK_UnitCommand]
    FOREIGN KEY ([Unit_Id])
    REFERENCES [dbo].[UnitSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UnitCommand'
CREATE INDEX [IX_FK_UnitCommand]
ON [dbo].[CommandSet]
    ([Unit_Id]);
GO

-- Creating foreign key on [Id] in table 'UnitSet_Camera'
ALTER TABLE [dbo].[UnitSet_Camera]
ADD CONSTRAINT [FK_Camera_inherits_Unit]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[UnitSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'UnitSet_Tellstick'
ALTER TABLE [dbo].[UnitSet_Tellstick]
ADD CONSTRAINT [FK_Tellstick_inherits_Unit]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[UnitSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

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

我的问题不是实体框架本身,而是简单的 SQL 语法。

在我的模型中,Camera实体继承自Unit。我想对相机表做一个INSERT这样:

INSERT INTO UnitSet_Camera
       (Login,
        Password,
        URL)
 VALUES
       ('myLoginName'
       ,'myPassword'
       ,'http://foo.bar:007');

我也知道这是不完整的,因为我必须以某种方式为我的相机实体提供一个“名称”(因为继承自表Unit及其属性“名称”)

完整的 SQLINSERT语句应该如何查找任意Camera实体?

4

1 回答 1

1

有两个表,因为它是继承的 - UnitSet 有一个在派生类中引用的 IDENTITY。基本上你需要这样做:

INSERT INTO dbo.UnitSet (Name) VALUES ('The Name');
INSERT INTO UnitSet_Camera
       (Login,
        Password,
        URL,
        Id)
 VALUES
       ('myLoginName'
       ,'myPassword'
       ,'http://foo.bar:007'
       ,SCOPE_IDENTITY());

但是还有其他的事情,比如OUTPUT可以在单个语句中完成,或者您可以制作一个显示相机所有属性的视图并使用触发器使其可更新

但是我想知道,当您显然决定通过 ORM 执行此类操作时,为什么要通过 SQL 执行此操作。

于 2012-06-25T20:27:03.507 回答