我可以获得实体框架(模型第一)来生成复合键吗? [英] Can I get Entity Framework (model-first) to generate composite keys?

查看:106
本文介绍了我可以获得实体框架(模型第一)来生成复合键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework的model-first模式设计一个数据库。基于我收到的好的反馈,我采用超类型/子类型的部分的数据库。



此模式需要至少2个表中的2列复合键(见下面的模式)。


$ b $我已经在论坛上搜索了实体框架和复合键,但是我发现的这些问题都没有在这个更基础的层面上:
我可以设置一个实体模型,以便它将使用2列上的复合主键生成一个表(使用从模型生成数据库),这样一个表是第二个表上的外键?而在另一张桌子上,除了FK之外,同样的情况基于第一张表中的2列?



或者,最好是让EF生成DB w / o复合键,然后进入SQL Server,(重新)设置主键,删除模型&根据新实例化的数据库创建一个新的模型?当然,我知道如何做到这一点;但是,由于我还在制作一个细微的细节,而是在DB结构中编写了2个细节,所以我更希望在这一点上推迟基本上在DB结构中产生烘焙的东西。



这是推荐的超类型/子类型结构,我已经在我的实体模型中镜像(w / o但是确定如何获取生成的复合键):

  CREATE TABLE出版物(
pub_id INTEGER NOT NULL PRIMARY KEY,
pub_type CHAR(1)CHECK(pub_type IN('A','B ','P','S')),
pub_url VARCHAR(64)NOT NULL UNIQUE,
CONSTRAINT publications_superkey UNIQUE(pub_id,pub_type)
);


CREATE TABLE文章(
pub_id INTEGER NOT NULL,
pub_type CHAR(1)DEFAULT'A'CHECK(pub_type ='A'),
占位符CHAR(1)NOT NULL, - 其他文章属性的占位符
PRIMARY KEY(pub_id,pub_type),
FOREIGN KEY(pub_id,pub_type)REFERENCES出版物(pub_id,pub_type)
);

CREATE TABLE stories(
pub_id INTEGER NOT NULL,
pub_type CHAR(1)DEFAULT'S'CHECK(pub_type ='S'),
placeholder CHAR 1)NOT NULL, - 其他故事属性的占位符
PRIMARY KEY(pub_id,pub_type),
FOREIGN KEY(pub_id,pub_type)REFERENCES出版物(pub_id,pub_type)
);


解决方案

我没有尝试,但我想你会不能用模型先创建它。原因是您的第一个表正在使用Unique约束,而您的第二个和第三个表正在基于该约束构建FK。实体框架不支持唯一约束,所以我的假设是不能生成这个DB模式。但是没有什么比较容易,只要试一试。


I am designing a database using Entity Framework's "model-first" approach. Based on excellent feedback I recieved here, I am adopting a super-type/sub-type pattern for part of the DB.

This pattern requires composite keys based on 2 columns in at least 2 tables (see schema below).

I've searched the forum for "entity framework" and "composite keys", but none of the questions I found were on this more basic level: Can I set up an entity model so that it will generate one table (using "Generate Database from Model...") with a composite primary key on 2 columns, such that one is a foreign key on a second table? And on another table, the same situation except the FK is based on 2 columns in the first table?

OR, is it better to just let EF generate the DB w/o the composite keys, then go into SQL Server, (re-)set the primary keys, delete the model, & create a new model based on the newly instantiated database? I know how to do that, of course; but as I'm still working out a minor detail or 2 in the DB structure, I'd prefer to put off anything that essentially results in baking in the DB structure at this point.

Here's the recommended super-type/sub-type structure, which I've mirrored in my entity model (w/o yet figuring out how to get the composite keys generated):

CREATE TABLE publications (
  pub_id INTEGER NOT NULL PRIMARY KEY,
  pub_type CHAR(1) CHECK (pub_type IN ('A', 'B', 'P', 'S')),
  pub_url VARCHAR(64) NOT NULL UNIQUE,
  CONSTRAINT publications_superkey UNIQUE (pub_id, pub_type)
);


CREATE TABLE articles (
  pub_id INTEGER NOT NULL,
  pub_type CHAR(1) DEFAULT 'A' CHECK (pub_type = 'A'),
  placeholder CHAR(1) NOT NULL, -- placeholder for other attributes of articles
  PRIMARY KEY (pub_id, pub_type),
  FOREIGN KEY (pub_id, pub_type) REFERENCES publications (pub_id, pub_type)
);

CREATE TABLE stories (
  pub_id INTEGER NOT NULL,
  pub_type CHAR(1) DEFAULT 'S' CHECK (pub_type = 'S'),
  placeholder CHAR(1) NOT NULL, -- placeholder for other attributes of stories
  PRIMARY KEY (pub_id, pub_type),
  FOREIGN KEY (pub_id, pub_type) REFERENCES publications (pub_id, pub_type)
);

解决方案

I didn't try it but I think you will not be able to create this with model first. The reason is that your first table is using Unique constraint and your second and third table is building FK based on that constraint. Entity framework does not support Unique constraints so my assumption is that it will not be able to generate this DB schema. But nothing is more easy then simply try it.

这篇关于我可以获得实体框架(模型第一)来生成复合键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆