我可以使用实体框架(模型优先)来生成组合键吗?

发布于 2024-10-21 03:28:33 字数 1402 浏览 4 评论 0原文

我正在使用实体框架的“模型优先”方法设计一个数据库。基于我在这里收到的优秀反馈,我对部分采用超类型/子类型模式数据库的。

此模式需要基于至少 2 个表中的 2 列的复合键(请参见下面的架构)。

我在论坛中搜索了“实体框架”和“复合键”,但我发现的问题都不是这个更基本的级别: 我是否可以设置一个实体模型,以便它将生成一个表(使用“从模型生成数据库...”),该表在 2 列上具有复合主键,这样一个表是第二个表上的外键?在另一个表上,除了 FK 之外,相同的情况是基于第一个表中的 2 列?

或者,是否最好让 EF 生成不带组合键的数据库,然后进入 SQL Server,(重新)设置主键,删除模型,&基于新实例化的数据库创建新模型?我当然知道该怎么做;但由于我仍在研究数据库结构中的一个或两个小细节,因此我宁愿推迟任何实质上导致数据库结构烘焙的事情。

这是推荐的超类型/子类型结构,我已将其镜像到我的实体模型中(尚未弄清楚如何生成组合键):

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 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)
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2024-10-28 03:28:33

我没有尝试过,但我认为您将无法首先使用模型创建它。原因是您的第一个表正在使用 Unique 约束,而您的第二个和第三个表正在基于该约束构建 FK。实体框架不支持唯一约束,因此我的假设是它将无法生成此数据库模式。但没有什么比简单尝试更容易的了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文