【问题标题】:Is it possible to add Properties to an Entity Context and therefore it's connected database table是否可以将属性添加到实体上下文,因此它是连接的数据库表
【发布时间】:2015-03-03 20:49:52
【问题描述】:

我有客户可以在其应用程序的列表中添加或删除的字段,因此需要将它们添加到数据库/上下文中以供以后使用。

例如,我有一个模型优先实体上下文dbContext,而有问题的模型是Customersproperties id、名称、访问级别...

我想知道是否有办法以编程方式将属性添加到实体模型/或上下文,或者我是否需要对动态数据库字段采取完全不同的方法?

例如:需要在其模型/数据库表中添加一个Inactive boolean 字段。

【问题讨论】:

    标签: c# dynamic entity-framework-6 add


    【解决方案1】:

    不,这绝对不可能。 EF 不支持它,即使可以,向模型添加新属性也需要每次都进行数据库迁移。如果您希望用户能够将自定义数据添加到 Cutomers 类,请考虑以下内容:

    public class Customers
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        //... more properties.
    
        public virtual ICollection<UserData> UserData { get; set; }
    }
    
    public class UserData
    {
        public string PropertyName { get; set; }
        public string PropertyValue { get; set; }
    }
    

    否则,您可以做这样的事情的唯一方法是使用像 MongoDB 这样的 NoSQL 数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2021-06-08
      • 2019-12-19
      • 1970-01-01
      相关资源
      最近更新 更多