I am a beginner and have just started experimenting with the modeling tool in Entity Framework 4.1.
The picture shows a diagram of my database and further down there are the automatically generated scripts to create my database (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
-- --------------------------------------------------
My question is not the Entity Framework itself, but simple SQL syntax.
In my model, the Camera entity inherits from Unit. I want to make an INSERT to the Camera table, something like this:
INSERT INTO UnitSet_Camera
(Login,
Password,
URL)
VALUES
('myLoginName'
,'myPassword'
,'http://foo.bar:007');
I also know that this is incomplete because I somehow have to provide my Camera entity with a “Name” (because of the inheritance from table Unit and its’ property “Name”)
How should a full SQL INSERT statement look for an arbitrary Camera entity?
There are two tables, because it’s inheritance – UnitSet has an IDENTITY which is referred to in the derived class. Basically you need to do this:
But there are other things like OUTPUT to do it in a single statement or you could make a view which exposed all the properties of camera and use a trigger to make it updateable.
But I’m wondering why you want to do this through SQL when you have obviously decided to go through an ORM to do this kind of operation.