使用实体框架和实体框架在运行时更改数据库代码优先

发布于 2024-12-03 16:22:36 字数 350 浏览 4 评论 0原文

有没有办法使用 Code First 方法来更改使用 EF 的底层数据库?

我有 2 个具有静态模型的表: 用户和信息1。

我还有另一个表,我将其称为 info2。

我希望能够从我网站的管理部分添加和删除 Info2 中的列。

我的目标是拥有一个可以动态更改的网站,可以根据用户的喜好添加和删除字段,而用户无需了解任何有关编码的知识。

我考虑过在 MVC3 项目模型中指定的数据库之外使用一个单独的数据库,并直接向该数据库发出 SQL 请求。

这也可以通过使用一个包含动态创建字段的表和另一个包含数据的表来完成,但这很快就会变得混乱。

有人做过这样的事吗?这是一个坏主意吗?

Is there any way to alter the underlying database using EF using Code First approach?

I have 2 tables which have a static model:
Users and Info1.

I also have another table which Ill call info2.

I would like to be able to add and remove columns from Info2 from the admin section of my website.

My goal is to have a website which can dynamically be altered as you go, adding and removing fields as the user likes, without the user having to know anything about coding.

I've considered using a separate database outside of the one specified in the model of my MVC3 project and do straight SQL requests to that instead.

This could also be accomplished by having a table with the dynamically created fields, and another with the data, but this gets messy fast.

Has anyone done anything like this? Is it a bad idea?

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

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

发布评论

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

评论(1

是你 2024-12-10 16:22:36

我建议不要尝试水平扩展表格,这是您应该做出有意识的决定的操作。

相反,我建议您将值存储为名称/值对。您可以拥有具有特定类型的表(假设您需要一个与键配对的整数值),然后您可以将它们选择到用户的字典中。

如果您担心复制键值,您还需要一个包含键的表。

例如,您将有一个 UserDefinedKey

UserDefinedKeyId (int, PK)     Key (varchar(?))
--------------------------     ----------------
                         1     'My Website'
                         2     'My favorite color'

然后您将有一个 UserDefinedString 表(用于字符串值)

UserDefinedStringId  UserId     UserDefinedKeyId  Value
(int, PK)            (int, FK)  (int, FK)         (varchar(max))
-------------------  ---------  ----------------  --------------
                  1          1                 1  'http://stackoverflow.com'
                  2          1                 2  'Blue'
                  3          2                 2  'Red'

您可能希望在 >UserIdUserDefinedKeyId 字段,以防止人们为同一键输入多个值(如果您想要这样,请拥有一个没有唯一约束的单独表)。

然后,当您想要为用户添加值时,可以将其添加到 UserDefinedKey 表中,然后将您的逻辑从该表和保存这些值的其他表中驱动出来。

垂直存储值的另一个好处是,您不会浪费那些不被所有用户使用的值的列的空间。

例如,假设您采用修改表格的方法,对于上述属性,您将得到:

UserId  WebSite                   Color
------  -------                   -----
     1  http://stackoverflow.com  Blue
     2  (null)                    Red

现在假设第三个用户出现,并添加了一个最喜欢的运动队值,他们是只有一个人使用它,该表看起来像这样:

UserId  WebSite                   Color  FavoriteSportsTeam
------  -------                   -----  ------------------
     1  http://stackoverflow.com  Blue   (null)
     2  (null)                    Red    (null)
     3  (null)                    (null) Yankees

随着用户和属性数量的增长,您拥有的稀疏数据量将急剧增加。

现在,假设您使用的是 SQL Server 2008,您可以使用 稀疏列,如果不这样做,你的表将会变得很大,但数据却很少。

此外,使用稀疏列并不能消除使用数据定义语言(DDL)的事实 动态更改架构。

此外,实体框架将无法调整其对象模型来适应新属性;每次添加属性时,您都必须将该属性添加到对象模型中,重新编译并重新部署。

使用垂直方法,当然需要更多的工作,但它将具有无限的灵活性,并且可以更有效地利用数据库空间。

I'd recommend not trying to expand the table horizontally, that's an operation that you should make a conscious decision to have.

Instead, I'd recommend that you store the values as name/value pairs. You can have tables that have specific types (let's say you needed an integer value paired with a key), and then you would select those into a dictionary for the user.

You'd also have a table which has the keys, if you are concerned about replicating key values.

For example, you'd have a UserDefinedKey table

UserDefinedKeyId (int, PK)     Key (varchar(?))
--------------------------     ----------------
                         1     'My Website'
                         2     'My favorite color'

Then you would have a UserDefinedString table (for string values)

UserDefinedStringId  UserId     UserDefinedKeyId  Value
(int, PK)            (int, FK)  (int, FK)         (varchar(max))
-------------------  ---------  ----------------  --------------
                  1          1                 1  'http://stackoverflow.com'
                  2          1                 2  'Blue'
                  3          2                 2  'Red'

You'd probably want to place a unique index on the UserId and UserDefinedKeyId fields to prevent people from entering multiple values for the same key (if you want that, have a separate table without the unique constraint).

Then, when you want to add a value for users, you add it to the UserDefinedKey table, and then drive your logic off that table and the other tables which hold the values.

Another benefit of storing the values vertically is that you aren't wasting space for columns with values that aren't being used by all users.

For example, assuming you take the approach of modifying the table, for the attributes above, you would get:

UserId  WebSite                   Color
------  -------                   -----
     1  http://stackoverflow.com  Blue
     2  (null)                    Red

Now let's say a third user comes along, and adds a Favorite Sports Team value, and they are the only one who uses it, the table then looks like:

UserId  WebSite                   Color  FavoriteSportsTeam
------  -------                   -----  ------------------
     1  http://stackoverflow.com  Blue   (null)
     2  (null)                    Red    (null)
     3  (null)                    (null) Yankees

As the number of users and attributes grows, the amount of sparse data that you have will increase dramatically.

Now, assuming you are using SQL Server 2008, you could use sparse columns, if you don't, your table is going to get huge but not have much data.

Also, using sparse columns doesn't take away from the fact that it's pretty dirty to use data definition language (DDL) to change the schema on the fly.

Additionally, Entity Framework isn't going to be able to adapt it's object model to account for the new attributes; every time you have an attribute added, you will have to go and add the attribute to your object model, recompile, and redeploy.

With a vertical approach, it takes more work, granted, but it will be infinitely flexible, as well as utilize your database space more efficiently.

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