How can I handle an account – product relationship in ASP.NET Core?

huangapple go评论74阅读模式
英文:

How can I handle an account - product relationship in ASP.NET Core?

问题

我正在尝试创建一个类似于账户(身份)可以拥有多个产品,但产品只能有一个账户的关系。我将DbContext和数据库分开,但是当我尝试将第二个产品添加到具有相同账户的DbContext时,我会收到关于“账户主键重复”的错误。

我应该如何处理这些关系?我应该使用分开的DbContext还是只使用一个DbContext

英文:

I am trying to create a relationship like accounts (identity) can have multiple products but products can only have only one account. I separated DbContext and databases by two but when I try to add second product to the DbContext with the same account, I get an error about "account primary key duplication".

How can I handle these relationship? Should I use separated DbContext or just have one instead?

答案1

得分: 2

时间来写文档了。你到底在想什么?没有理由需要多个数据库上下文。

但是,当我尝试使用相同的账户将第二个产品添加到数据库上下文时,它会报告账户主键重复的错误。

文档和示例会比我们调试你没有与我们分享的代码更有帮助。

你应该使用分离的数据库上下文还是只使用一个呢?

嗯,你有什么理由要维护一个噩梦?有一些情况下使用多个数据库上下文是有意义的(比如:访问具有不同模式的分离数据库或者两个独立的应用程序共享一个数据库),但一般来说这会让人摸不着头脑。一个数据库,一个数据库上下文(在许多情况下会自动生成)。

英文:

> I seperated db contexts and databases by two

Time for documentation. What the heck did you even think doing that? There is no reason for more than one db context.

> but when I try to add second product to the db context with the same account,
> it gives a account primary key duplication error.

Documentation and examples. These will help more than us debugging code you do not bother to share with us.

> Should I use separated db contexts or just have one instead?

Well, what is your reason for a maintenance nightmare? There are some scenarios more than one db context makes sense (like: Accessing separate databases with separate schemata or 2 separate apps sharing one database), but generally it falls straight into "wtf" territory. One database, one db context (automatically generated in many cases).

huangapple
  • 本文由 发表于 2023年8月9日 04:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定