How do I perform a migration and have just my existing Entities visualised?
The problem is:
I removed all versions files from migrations
I dropped schema public
I created new schema public
I made a make:migration
I made a doctrine:migrations:migrate
And old data appeared - I mean tables which don't have its entities and were dropped long time ago.
Schema is created, but with reduntant and unnecessary data.
How to totally reset migrations? Is there any cache which save dropped tables (not considering versions of migrations)?
Here is my latest migrations file - I want to make a migration and I don't have such tables as for instance user_to_offert_user.
Don't give me manual solutions, please - I know that I can modify it before migration, but it seems pointless to me doing that every time when I will change something.
If something is not clear, ask me please. I tried to specify problem as much as I can.
Additional info:
What I am exactly doing:
I removed all versions from migrations.
I truncated all data
I dropped my public schema
I created my new public schema
I run bin/console make:migration
I run bin/console doctrine:migrations:migrate
And here is my huge problem:
I have this kind of entities:
And when I run migrate all of this tables shows:
As as you can see, there are too many tables. Some of them I dropped a long time ago and they still appears in every migration.
I don't know how to get by and I am sooo desperate, because I have been spending on this problem 3th day.
I have my database on Heroku - I use Postgres and when I dropped all tablesthey doesn't show in heroku:
Here is code of this migration:
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210403092535 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SEQUENCE requirement_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE skill_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE "user_id_seq" INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE user_to_offert_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE login_history (id SERIAL NOT NULL, user_id_id INT NOT NULL, login_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, login_status BOOLEAN NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_37976E369D86650F ON login_history (user_id_id)');
$this->addSql('CREATE TABLE offert (id SERIAL NOT NULL, event_firm_id INT NOT NULL, event_name VARCHAR(100) NOT NULL, date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, description VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_44229184CF8D1CF ON offert (event_firm_id)');
$this->addSql('CREATE TABLE requirement (id INT NOT NULL, requirement VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE requirement_user (requirement_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(requirement_id, user_id))');
$this->addSql('CREATE INDEX IDX_598D28707B576F77 ON requirement_user (requirement_id)');
$this->addSql('CREATE INDEX IDX_598D2870A76ED395 ON requirement_user (user_id)');
$this->addSql('CREATE TABLE skill (id INT NOT NULL, skill VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE skill_user (skill_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(skill_id, user_id))');
$this->addSql('CREATE INDEX IDX_CAD24AFB5585C142 ON skill_user (skill_id)');
$this->addSql('CREATE INDEX IDX_CAD24AFBA76ED395 ON skill_user (user_id)');
$this->addSql('CREATE TABLE "user" (id INT NOT NULL, user_details_id_id INT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, account_type VARCHAR(100) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D64912595E8 ON "user" (user_details_id_id)');
$this->addSql('CREATE TABLE user_details (id SERIAL NOT NULL, name VARCHAR(255) NOT NULL, surname VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE user_to_offert (id INT NOT NULL, hired BOOLEAN NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE user_to_offert_offert (user_to_offert_id INT NOT NULL, offert_id INT NOT NULL, PRIMARY KEY(user_to_offert_id, offert_id))');
$this->addSql('CREATE INDEX IDX_4AA1878F8C2827E4 ON user_to_offert_offert (user_to_offert_id)');
$this->addSql('CREATE INDEX IDX_4AA1878F3D478C97 ON user_to_offert_offert (offert_id)');
$this->addSql('CREATE TABLE user_to_offert_user (user_to_offert_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(user_to_offert_id, user_id))');
$this->addSql('CREATE INDEX IDX_7BE6CF748C2827E4 ON user_to_offert_user (user_to_offert_id)');
$this->addSql('CREATE INDEX IDX_7BE6CF74A76ED395 ON user_to_offert_user (user_id)');
$this->addSql('ALTER TABLE login_history ADD CONSTRAINT FK_37976E369D86650F FOREIGN KEY (user_id_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE offert ADD CONSTRAINT FK_44229184CF8D1CF FOREIGN KEY (event_firm_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE requirement_user ADD CONSTRAINT FK_598D28707B576F77 FOREIGN KEY (requirement_id) REFERENCES requirement (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE requirement_user ADD CONSTRAINT FK_598D2870A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE skill_user ADD CONSTRAINT FK_CAD24AFB5585C142 FOREIGN KEY (skill_id) REFERENCES skill (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE skill_user ADD CONSTRAINT FK_CAD24AFBA76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE "user" ADD CONSTRAINT FK_8D93D64912595E8 FOREIGN KEY (user_details_id_id) REFERENCES user_details (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_offert ADD CONSTRAINT FK_4AA1878F8C2827E4 FOREIGN KEY (user_to_offert_id) REFERENCES user_to_offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_offert ADD CONSTRAINT FK_4AA1878F3D478C97 FOREIGN KEY (offert_id) REFERENCES offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_user ADD CONSTRAINT FK_7BE6CF748C2827E4 FOREIGN KEY (user_to_offert_id) REFERENCES user_to_offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_user ADD CONSTRAINT FK_7BE6CF74A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE user_to_offert_offert DROP CONSTRAINT FK_4AA1878F3D478C97');
$this->addSql('ALTER TABLE requirement_user DROP CONSTRAINT FK_598D28707B576F77');
$this->addSql('ALTER TABLE skill_user DROP CONSTRAINT FK_CAD24AFB5585C142');
$this->addSql('ALTER TABLE login_history DROP CONSTRAINT FK_37976E369D86650F');
$this->addSql('ALTER TABLE offert DROP CONSTRAINT FK_44229184CF8D1CF');
$this->addSql('ALTER TABLE requirement_user DROP CONSTRAINT FK_598D2870A76ED395');
$this->addSql('ALTER TABLE skill_user DROP CONSTRAINT FK_CAD24AFBA76ED395');
$this->addSql('ALTER TABLE user_to_offert_user DROP CONSTRAINT FK_7BE6CF74A76ED395');
$this->addSql('ALTER TABLE "user" DROP CONSTRAINT FK_8D93D64912595E8');
$this->addSql('ALTER TABLE user_to_offert_offert DROP CONSTRAINT FK_4AA1878F8C2827E4');
$this->addSql('ALTER TABLE user_to_offert_user DROP CONSTRAINT FK_7BE6CF748C2827E4');
$this->addSql('DROP SEQUENCE requirement_id_seq CASCADE');
$this->addSql('DROP SEQUENCE skill_id_seq CASCADE');
$this->addSql('DROP SEQUENCE "user_id_seq" CASCADE');
$this->addSql('DROP SEQUENCE user_to_offert_id_seq CASCADE');
$this->addSql('DROP TABLE login_history');
$this->addSql('DROP TABLE offert');
$this->addSql('DROP TABLE requirement');
$this->addSql('DROP TABLE requirement_user');
$this->addSql('DROP TABLE skill');
$this->addSql('DROP TABLE skill_user');
$this->addSql('DROP TABLE "user"');
$this->addSql('DROP TABLE user_details');
$this->addSql('DROP TABLE user_to_offert');
$this->addSql('DROP TABLE user_to_offert_offert');
$this->addSql('DROP TABLE user_to_offert_user');
}
}
Doctrine MigrationsBundle has a table where it stores the information which migrations previously ran. If you reset your migrations you will also have to reset that table. You can use the rollup command for this:
php bin/console doctrine:migrations:rollup
If this does not work you might want to change the table manually, e.g. in a migration, but you should probably avoid this if possible. By default the table is called doctrine_migration_versions, but you can use the debug:config command to check if your configuration is different:
php bin/console debug:config doctrine_migrations storage
Here is how it should look by default:
doctrine_migrations:
# ...
storage:
# Default (SQL table) metadata storage configuration
table_storage:
table_name: 'doctrine_migration_versions'
version_column_name: 'version'
version_column_length: 1024
executed_at_column_name: 'executed_at'
Related
I can not add a referential constraint, both tables has the two columns id and version as primary key. I will only check the integrity that the row is existing without checking version.
CREATE TABLE TABLEE
(ID INTEGER NOT NULL,
VERSION INTEGER NOT NULL,
PRIMARY KEY (ID , VERSION);
CREATE TABLE CHAIR
(ID INTEGER NOT NULL,
VERSION INTEGER NOT NULL,
ID_TABLEE INTEGER,
PRIMARY KEY (ID , VERSION);
ALTER Chair
ADD constraint MYC FOREIGN KEY (ID)
REFERENCES TABLEE (ID)
ON DELETE RESTRICT
Got this error
[Code: -573, SQL State: 42890] A column list specified in the references clause of constraint "MYC " does not identify a unique constraint of the parent table or nickname "TABLEE".. SQLCODE=-573, SQLSTATE=42890, DRIVER=4.28.11
The problem here is that Tablee its primary key consists of two columns id and version. But the integrity check should only be made on the referenced id, not version.
To establish a foreign key on a table the referenced column needs to:
Be a primary key on the other table.
...or at least act as one.
Since you already have a primary key on the referenced table, you can use the second approach and add a UNIQUE and NOT NULL constraints on ID.
For example:
CREATE TABLE TABLEE (
ID INTEGER NOT NULL,
VERSION INTEGER NOT NULL,
PRIMARY KEY (ID , VERSION),
constraint uq1 unique (id) -- added UNIQUE constraint on ID
);
CREATE TABLE CHAIR (
ID INTEGER NOT NULL,
VERSION INTEGER NOT NULL,
ID_TABLEE INTEGER,
PRIMARY KEY (ID , VERSION)
);
ALTER table Chair
ADD constraint MYC FOREIGN KEY (ID)
REFERENCES TABLEE (ID)
ON DELETE RESTRICT;
See running example at db<>fiddle.
Note: If you want ID to have repeated values over the table, then ID is not a key, and cannot be referenced as one.
See: SQLFiddle
I am having a many-to-many relationship as such:
CREATE TABLE IF NOT EXISTS store (
id BIGSERIAL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS collection (
id BIGSERIAL PRIMARY KEY,
store_id bigint NOT NULL,
FOREIGN KEY (store_id) REFERENCES store (id)
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS item (
id BIGSERIAL PRIMARY KEY,
store_id bigint NOT NULL,
FOREIGN KEY (store_id) REFERENCES store (id)
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS collection_item (
id BIGSERIAL PRIMARY KEY,
collection_id bigint NOT NULL,
item_id bigint,
UNIQUE (collection_id, item_id),
FOREIGN KEY (collection_id) REFERENCES collection (id)
ON DELETE CASCADE,
FOREIGN KEY (item_id) REFERENCES item (id)
ON DELETE SET NULL
);
INSERT INTO store (id) VALUES (1);
INSERT INTO item (id, store_id) VALUES (1, 1);
INSERT INTO collection (id, store_id) VALUES (1, 1);
INSERT INTO collection_item (id, collection_id, item_id) VALUES (DEFAULT, 1, 1);
My problem is that deleting a store
DELETE FROM store WHERE store.id = 1;
will give the following error:
ERROR: update or delete on table "store" violates foreign key constraint
"collection_store_id_fkey" on table "collection"
Detail: Key (id)=(1) is still referenced from table "collection".
I understand the problem but I don't know how I can resolve this issue.
If an item gets deleted, the relation in collection_item should not be removed but the item_id should be set to NULL. On the other hand, if a collection gets deleted, all related collection_item records should be removed as well.
How can DELETE CASCADE work in such a setup or do I have to model my tables differently if I need this kind of behavior?
You can just add ON DELETE CASCADE where you have ON UPDATE CASCADE
CREATE TABLE IF NOT EXISTS collection (
id BIGSERIAL PRIMARY KEY,
store_id bigint NOT NULL,
FOREIGN KEY (store_id) REFERENCES store (id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS item (
id BIGSERIAL PRIMARY KEY,
store_id bigint NOT NULL,
FOREIGN KEY (store_id) REFERENCES store (id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
SQLFiddle
To resolve this issue you can add ON DELETE behavior to the store_id column of the collection table (and that of the item table). Basically you can just change the ON UPDATE to ON DELETE in both of these table definitions, since I doubt you'll be updating the id column for existing rows in the store table. You also mention the ON DELETE behavior for the item_id column of collection_item, if you want the column value to become NULL when an item is deleted, you can use ON DELETE SET NULL.
CREATE TABLE IF NOT EXISTS store (
id BIGSERIAL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS collection (
id BIGSERIAL PRIMARY KEY,
store_id bigint NOT NULL,
FOREIGN KEY (store_id) REFERENCES store (id)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS item (
id BIGSERIAL PRIMARY KEY,
store_id bigint NOT NULL,
FOREIGN KEY (store_id) REFERENCES store (id)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS collection_item (
id BIGSERIAL PRIMARY KEY,
collection_id bigint NOT NULL,
item_id bigint NOT NULL,
UNIQUE (collection_id, item_id),
FOREIGN KEY (collection_id) REFERENCES collection (id)
ON DELETE CASCADE,
FOREIGN KEY (item_id) REFERENCES item (id)
ON DELETE SET NULL
);
On this case, you have to delete table refrences before to delete stores id.
Or to do the modifications above.
DELETE FROM item where store_id= 1;
DELETE FROM collection where store_id = 1;
DELETE FROM store where id = 1;
I try to create code for postgresql
CREATE TABLE kaart (
kaartid integer NOT NULL,
naam character varying,
saldo real,
kaarthouderid integer
);
CREATE TABLE kaart_product (
kaartkaartid integer,
productid2 integer
);
CREATE TABLE kaarthouder (
id integer NOT NULL,
naam character varying(255),
naw character varying(255),
geslacht "char"
);
CREATE TABLE product (
naam character varying,
id integer NOT NULL
);
ALTER TABLE ONLY kaart
ADD CONSTRAINT kaart_pkey PRIMARY KEY (kaartid);
ALTER TABLE ONLY kaarthouder
ADD CONSTRAINT kaarthouder_pkey PRIMARY KEY (id);
ALTER TABLE ONLY product
ADD CONSTRAINT product_pkey PRIMARY KEY (id);
ALTER TABLE ONLY kaart
ADD CONSTRAINT kaartco FOREIGN KEY (kaartid) REFERENCES kaarthouder(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE ONLY kaart_product
ADD CONSTRAINT kaartkaartidco FOREIGN KEY (kaartkaartid) REFERENCES kaart(kaartid) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE ONLY kaart_product
ADD CONSTRAINT productidco FOREIGN KEY (kaartkaartid) REFERENCES product(id) ON UPDATE CASCADE ON DELETE CASCADE;
INSERT INTO kaart VALUES (1, 'Sander',50.00 ,1);
INSERT INTO kaart_product VALUES (1,1);
INSERT INTO kaarthouder VALUES (1, 'Sander','test,testing','man');
INSERT INTO product VALUES ('studentenproduct',1);
But whenever i try to run it it gives me this error:
23503: insert or update on table "kaart" violates foreign key constraint "kaartco"
But i really dont know why this happens since it is the same to the other foreign keys that are below it
So can someone help me fix this?
You try to link to a product and a kaart that doesn't exist yet.
Move:
INSERT INTO kaart_product VALUES (1,1);
Two lines down under:
INSERT INTO product VALUES ('studentenproduct',1);
That should do the job for you.
Try to search google for forgein key contstraints and how they work.
I have defined these tables:
CREATE TABLE "public".category (id BIGSERIAL NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id));
CREATE UNIQUE INDEX category_name ON "public".category (name);
CREATE TABLE "public".clusters (id BIGSERIAL NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id));
CREATE INDEX clusters_name ON "public".clusters (name);
CREATE TABLE "public".keywords (id BIGSERIAL NOT NULL, text varchar(255) NOT NULL, category_id int8 NOT NULL, top_results int4, cluster_id int8, month_requests int4, click_cost float8, PRIMARY KEY (id));
CREATE INDEX keywords_text ON "public".keywords (text);
ALTER TABLE "public".keywords ADD CONSTRAINT FKkeywords488682 FOREIGN KEY (cluster_id) REFERENCES "public".clusters (id);
ALTER TABLE "public".keywords ADD CONSTRAINT FKkeywords446526 FOREIGN KEY (category_id) REFERENCES "public".category (id) ON UPDATE CASCADE ON DELETE CASCADE;
added one record to category table:
INSERT INTO "public".category(id, name) VALUES (1, 'Test');
And now when I try to add record to keyword table
insert into "public"."keywords" ( "category_id", "text") values ( 1, 'testkey')
I got error:
ERROR: constraint 16959 is not a foreign key constraint
When I do
select * FROM pg_constraint;
I can't see constraint with this id. I can't understand what is the cause of this problem.
I've got a problem I can't figure out how to express with CTP5 of Code First and EF4.
EDIT: Added my old schema at the bottom that I'd like to replicate through CF
Here's my specific scenario:
A Team is an abstract concept; it should exist with a specific set of players, in a specific division, in a specific season.
A concrete example of this in action from the NFL:
1996 (season) AFC Central (division) Houston Oilers (team)
1997 (season) AFC Central (division) Tennessee Oilers (team)
1999 (season) AFC Central (division) Tennessee Titans (team)
2002 (season) AFC South (division) Tennessee Titans (team)
These are all the same team. I want to be able to do the following:
// Titans team id = 17
var myTeam = myContext.Teams.Single(t => t.Id == 17)
// display players
foreach (var p in myTeam.Seasons[1999].Players)
{
// Do something with the p here
}
// Display current division
Response.Write(myTeam.Seasons[2002].Division.Name);
I'm not sure of the specific query syntax within an ICollection member variable of myTeam.Seasons, but the concept should be the same none the less.
Can anyone shed some light on how you'd express this concept through CF in EF4 CF CTP5?
How would you express this through Code First?
Current SQL tables
CREATE TABLE dbo.Season
(
Id INT IDENTITY(1,1) NOT NULL,
LeagueId INT NOT NULL,
[Name] NVARCHAR(50) NOT NULL,
[Year] CHAR(4) NOT NULL,
PrevSeasonId INT NULL
) ON [PRIMARY];
// Primary key
ALTER TABLE dbo.Season WITH NOCHECK ADD
CONSTRAINT PK_Season PRIMARY KEY NONCLUSTERED (Id) ON [PRIMARY];
CREATE TABLE dbo.Division
(
Id INT IDENTITY(1,1) NOT NULL,
DefaultName NVARCHAR(50) NOT NULL
) ON [PRIMARY];
// Primary key
ALTER TABLE dbo.Division WITH NOCHECK ADD
CONSTRAINT PK_Division PRIMARY KEY NONCLUSTERED (Id) ON [PRIMARY];
// Key Relation Table
CREATE TABLE dbo.DivisionsInSeason
(
DivisionId INT NOT NULL,
SeasonId INT NOT NULL,
DefaultName NVARCHAR(50) NOT NULL,
Commissioner UNIQUEIDENTIFIER NOT NULL,
ParentDivId INT NULL
) ON [PRIMARY];
// Primary Key
ALTER TABLE dbo.DivisionsInSeason WITH NOCHECK ADD
CONSTRAINT PK_DivisionsInSeason PRIMARY KEY NONCLUSTERED (DivisionId, SeasonId) ON [PRIMARY] ;
// Foreign Keys
ALTER TABLE dbo.DivisionsInSeason WITH CHECK ADD
CONSTRAINT FK_DivisionsInSeason_Division FOREIGN KEY(DivisionId) REFERENCES dbo.Division(Id),
CONSTRAINT FK_DivisionsInSeason_Season FOREIGN KEY(SeasonId) REFERENCES dbo.Season(Id),
CONSTRAINT FK_DivisionsInSeason_User FOREIGN KEY(Commissioner) REFERENCES dbo.[User](Id);
CREATE TABLE dbo.Team
(
Id INT IDENTITY(1,1) NOT NULL,
DefaultName NVARCHAR(50) NOT NULL,
DefShortName NCHAR(3) NULL
) ON [PRIMARY];
// Primary key
ALTER TABLE dbo.Team WITH NOCHECK ADD
CONSTRAINT PK_Team PRIMARY KEY NONCLUSTERED (Id) ON [PRIMARY] ;
// Key relationship table
CREATE TABLE dbo.TeamsInDivision
(
TeamId INT NOT NULL,
DivisionId INT NOT NULL,
SeasonId INT NOT NULL,
GeneralManager UNIQUEIDENTIFIER NOT NULL,
Name NVARCHAR(50) NOT NULL,
ShortName NCHAR(3) NULL
) ON [PRIMARY];
// Check Constraints
ALTER TABLE dbo.TeamsInDivision ADD
CONSTRAINT PK_TeamsInDivision PRIMARY KEY NONCLUSTERED (TeamId, DivisionId, SeasonId) ON [PRIMARY];
// Foreign Keys
ALTER TABLE dbo.TeamsInDivision WITH CHECK ADD
CONSTRAINT FK_TeamsInDivision_Team FOREIGN KEY(TeamId) REFERENCES dbo.Team(Id),
CONSTRAINT FK_TeamsInDivision_Division FOREIGN KEY(DivisionId) REFERENCES dbo.Division(Id),
CONSTRAINT FK_TeamsInDivision_Season FOREIGN KEY(SeasonId) REFERENCES dbo.Season(Id),
CONSTRAINT FK_TeamsInDivision_User FOREIGN KEY(GeneralManager) REFERENCES dbo.[User](Id);
Maybe this could be usefull
http://blogs.microsoft.co.il/blogs/gilf/archive/2011/08/01/creating-a-many-to-many-mapping-using-code-first.aspx